Thomas B
-
Content Count
3 -
Joined
-
Last visited
Posts posted by Thomas B
-
-
On 2/11/2023 at 7:28 PM, aehimself said:Do not watch Task Manager to diagnose memory leaks. Windows often doesn't release the memory what the application is not using anymore but keeps it to speed up future memory allocations.
I don't know if FastMM (ReportMemoryLeaksOnShutdown) will report a leak in a DLL, but you can request a trial of DeLeaker which will show you everything for sure.
There's one thing for sure - there's no memory leak in System.SysUtils in the initialization / finalization section.
I did not only use Task Manager, I also used the built-in memory profiler in Visual Studio.
I know that Windows keeps around some memory. I did the same test with a DLL written in C/C++ and the memory usage grows up to around ~300KB then further LoadLibrary/FreeLibrary calls seem to reuse previous memory and the overall memory usage does not continue to grow. But that is not the case for the Delphi DLL. After running it for half an hour or so, the memory usage was already at a few houndred MB and it showed no sign of stopping.
I tried using FastMM (ReportMemoryLeaksOnShutdown), but it did not show any leaks. Visual Studio memory profiler shows the allocations for each call but it only hints at "external code" and some memory address. According to the memory snapshots there are around ~5KB of memory allocations for each LoadLibrary call, that are not freed with FreeLibrary.
All I can say for sure is that if I remove "uses System.SysUtils" the problem is gone, which is obviously not a fix, since I want to use some of the functionality.
-
I have a weird problem with a memory leak caused by a DLL written in Delphi.
As a test I have a C++ application that loads and unloads the DLL in a loop:
HINSTANCE lib; while(1) { lib = LoadLibraryA("MyLib.dll"); FreeLibrary(lib); }
When I look at the memory usage either in Task Manager or Visual Studio Memory Diagnostics, I can see that memory usage keeps growing indefinitely.
Originally I thought it was caused by a leak in my Delphi code. But eventually I reduced it to:
library MyLib; uses System.SysUtils; end.
And the memory leak still appears. Only if I remove "uses System.SysUtils;" the leak is gone.
I have tried to compile the DLL with Delphi XE2 and Delphi 11. The behaviour is the same.
Does anyone know what causes this? Is there anything I can do (maybe some compiler settings?) to avoid the memory leak?
Memory Leak when loading/unloading Delphi DLL
in Windows API
Posted · Edited by Thomas B
Thank you, I didn't know that existed. I will create a bug report and link it here when done.
Yes I just recreated the same behaviour with the host application written in Delphi. Here is the full code: