For a web framework I tried anonymous methods for the first time and ran into a problem with the memory management.
How can this memory leak (Delphi 2009) get fixed?
The leak message is:
13 – 20 bytes: Project27$ActRec x 1
program Project27;
type
TTestProc = reference to procedure;
procedure CallMe(Proc: TTestProc);
begin
end;
begin
CallMe(procedure begin end);
ReportMemoryLeaksOnShutdown := True;
end.
The same leak message “Project27$ActRec x 1” appears no matter how many anonymous methods are between begin and end, I guess that the leak is for the TTestProc type, not the individual anonymous procedures
program Project27;
type
TTestProc = reference to procedure;
procedure CallMe(Proc: TTestProc);
begin
end;
begin
ReportMemoryLeaksOnShutdown := True;
CallMe(procedure begin end);
CallMe(procedure var A: Integer; begin A := 42 ; end);
end.
When you declare an anonymous method inside a procedure or function, it gets cleaned up when that routine goes out of scope. (This is an oversimplification, but it’s good enough for the current discussion.) The problem is that the DPR’s main routine does not “go out of scope.” Instead, the Delphi compiler inserts a hidden call to
System.Haltat the end of it, which never returns.So if you write it this way, you’re going to get the memory leak notification. You can fix it by putting the anonymous method creation inside a routine that exits normally, like so: