Delphi Memory Issue (FastMM4)
Asked Answered
P

1

7

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.
Pathe answered 29/9, 2011 at 16:7 Comment(3)
I'm curious - why are you using a "reference to function" rather than a simple function type ? This would avoid the memory leak since it wouldn't involve the compiler jumping through hoops to achieve what can be achieved more simply. Or is this one of those cases where the simplified example doesn't fully reflect the original scenario ?Coulisse
just like the relative looseness of the typing with it so the variable can take procedures, methods or anonymous methods. It's being used for a framework so want to make it as extensible as possible.Pathe
I'm not sure this achieves that though, does it ? The reference still has to be to a function of the appropriate signature, no? If not then I'd say it's a reason to avoid this sort of thing like the plague !! if I declare a type as being a function with a certain signature then it jolly well better be used as such. Anything else is asking for a maintenance headache (not to mention confusion). And the question remains, why do you want to allow this flexibility, other than because you can/want to ? What purpose does the flexibility serve in your framework ?Coulisse
A
5

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.

Alysonalysoun answered 29/9, 2011 at 16:39 Comment(1)
There seems to be an extra call to IntfCopy that results in the reference count being set to 2. No idea why that is so.Inulin

© 2022 - 2024 — McMap. All rights reserved.