Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous method in project leaks memory

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. 
like image 253
mjn Avatar asked Feb 14 '12 18:02

mjn


People also ask

What happens when memory is leaked?

A memory leak reduces the performance of the computer by reducing the amount of available memory. Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly, the application fails, or the system slows down vastly due to thrashing.

What causes a memory leak in C#?

A memory leak may happen when your app references objects that it no longer needs to perform the desired task. Referencing said objects makes the garbage collector to be unable to reclaim the memory used, often resulting in performance degradation and potentially end up throwing an OutOfMemoryException.

Do infinite loops cause memory leaks?

Stack memory leaks occur when a method keeps getting called but never exits. This can happen if there is an infinite loop or if the method is being called with different data each time but the data is never used. Eventually, the stack will fill up and the program will run out of memory.

Is memory leak possible in C#?

If you have implemented a very long-running or infinite running thread that is not doing anything and it holds on to objects, you can cause a memory leak as these objects will never be collected. The fix for this is to be very careful with long-running threads and not hold on to objects not needed.


1 Answers

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.Halt at 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:

program Project27;

type
  TTestProc = reference to procedure;

  procedure CallMe(Proc: TTestProc);
  begin
  end;

  procedure Test;
  begin
    CallMe(procedure begin end);
  end;

begin
  Test;
  ReportMemoryLeaksOnShutdown := True;
end.
like image 124
Mason Wheeler Avatar answered Oct 12 '22 09:10

Mason Wheeler