Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi forms.pas memory leak?

HI

I have a dynamic link library writting in Delphi 2006 that has forms.pas in its uses clause.

If I load the dll and then immediately unload it in a for loop, say 10000 times, the memory slowly climbs. However if I take Forms.pas out of the uses clause of the dll then the problem goes away.

The code is very simple

Here is my code for the dll:

library Project1;

uses
  Forms;

begin

end.

Here is my code for the calling application:

procedure TForm1.Button1Click(Sender: TObject);
var
  t_ImportHandle: LongInt;
  t_Index: Integer;
begin
  for t_Index := 0 to 10000 - 1 do
  begin
    t_ImportHandle := LoadLibrary('Project1.dll');
    FreeLibrary(t_ImportHandle);
  end;
 end;

Is anyone else able to replicate this or know what the cause is and how to fix it?

like image 522
There is no spoon Avatar asked Dec 01 '25 17:12

There is no spoon


1 Answers

TApplication.Create uses the MakeObjectInstance function in Classes.pas. MakeObjectInstance allocates a 4KB buffer using VirtualAlloc, but doesn't free it, so each time you load/unload the DLL it's going to leak that much. Andreas Hausladen used to have a blog post about it, but it looks like he's taken it down. There's a fix posted on CodeCentral, and it's also included as part of Andreas's VCL Fix Pack package.

like image 147
Zoë Peterson Avatar answered Dec 03 '25 22:12

Zoë Peterson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!