Delphi-OpenCV icon indicating copy to clipboard operation
Delphi-OpenCV copied to clipboard

cvSetMemoryManager raised Exception

Open hafedh-trimeche opened this issue 5 years ago • 5 comments

Wanting intercept memory allocate & free, the cvSetMemoryManager is used. But exception raised: External exception E06D7363

function far_alloc(size:size_t;userdata:Pointer):Pointer;cdecl;
begin
  Result := AllocMem(Size);
end;

function far_free(pptr:Pointer;userdata:Pointer):Integer;cdecl;
begin
  Result := FreeMemory(pptr);
end;

constructor TOpenCV.Create;
begin
  inherited;
  if not MemoryManagerSet then
  begin
    cvSetMemoryManager(@far_alloc,@far_free,nil);
    MemoryManagerSet := True;
  end;
end;

hafedh-trimeche avatar Aug 28 '20 21:08 hafedh-trimeche

I think that the ways of working with memory (managers) in C and Delphi are different (always your "Captain Obvious"). In addition, inside the dll (for OpenCV), functions also allocate and free memory for their needs (for example, for auxiliary structures and, possibly, classes). The C allocator works by knowing how to properly allocate memory for C structures and how to properly initialize memory. The Delphi allocator doesn't know. We need to figure it out ...

Laex avatar Mar 12 '21 12:03 Laex

Hello, Thank you for your response.

But how to debug an eventual Memory Leak?

Best regards.

hafedh-trimeche avatar Mar 12 '21 12:03 hafedh-trimeche

First you need to try to establish where this memory leak occurs, for example, by setting ReportMemoryLeaksOnShutdown: = True; Additionally, you can use some "expert", for example EurekaLog or madExcept. They show detailed information about memory leaks.

Laex avatar Mar 12 '21 12:03 Laex

Dear,

This procedure is applicable to Memory allocated by Delphi via FastMM not for External DLL written into another language.

Would Alignment {$ALIGN} or/and {$ENUMSIZE} be the cause of the problem?

Best regards.

hafedh-trimeche avatar Mar 12 '21 12:03 hafedh-trimeche

cvSetMemoryManager installs memory manager for functions from OpenCV (i.e. for functions from DLL)

Laex avatar Mar 12 '21 13:03 Laex