Laex / Delphi-OpenCV

Project Delphi-OpenCV. Translation of OpenCV library header files in Delphi
500 stars 226 forks source link

cvSetMemoryManager raised Exception #135

Open hafedh-trimeche opened 4 years ago

hafedh-trimeche commented 4 years ago

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;
Laex commented 3 years ago

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 ...

hafedh-trimeche commented 3 years ago

Hello, Thank you for your response.

But how to debug an eventual Memory Leak?

Best regards.

Laex commented 3 years ago

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.

hafedh-trimeche commented 3 years ago

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.

Laex commented 3 years ago

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