Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Memory Issue (FastMM4)

Working on a project which uses factories to construct objects. I keep the pointers to the factory functions in vars globally (bad I know) and register them on initialization.

I recently was interested in seeing if the project had memory leaks so decided to download FastMM4 and have a look through. It came up with a few errors that I could fix but this one I'm a bit stumped on seems by me not freeing the memory related to the factory as shown in the code below I'm getting a small memory leak. Not ridiculous but annoying nevertheless.

What would I use to free the memory (if it is that) i've tried dispose(@factoryfunction) but seems to mangle everything. I'm not too good with low level pointer stuff always confuses the hell out of me so if someone could help that would be great.

I've included an example below that i've just written off the top of my head that illustrates the problem I'm having.

Cheers,

Barry

unit Test;

interface

uses classes;

type

TAFactoryFunction = reference to function (const aType : integer): TObject;

function testfunction (const aType : integer) : TObject;

implementation

function testfunction(const aType: integer) : TObject;
begin
    result := TObject.Create;
end;

var
   FactoryFunction : TAFactoryFunction

initialization
   FactoryFunction := testfunction;

finalization
   // possibly some freemem code here?

end.
like image 269
Barry Avatar asked Sep 29 '11 16:09

Barry


1 Answers

I just tested this in Delphi 2010 and it appears to be a bug. The compiler should generate code to clean that up, but it isn't. Even writing FactoryFunction := nil, as David suggested, doesn't work.

You should report this in QC as an error.

like image 100
Mason Wheeler Avatar answered Nov 02 '22 18:11

Mason Wheeler