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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With