Warning: This site is under construction, most links will be broken.
Programming -> The freepascal destroy memory leak
Last modified on Tue, 16th Feb 2010 at 22:15 GMT by zippletUpdate: As of FPC version 2.2.4, this bug has been fixed.
Freepascal is a very good open source object pascal compiler with a nice delphi emulation mode but it's delphi emulation is far from perfect. This particular issue drove me insane until I pinpointed it since it worked as expected under delphi but not under freepascal.
The following code leaks:
program leak;
uses classes;
type
tmyclass = class(tobject)
moo: array[0..1023] of integer; { to make the leak bigger }
procedure dodestroy;
end;
procedure tmyclass.dodestroy;
begin
destroy;
end;
var
m: tmyclass;
begin
m := tmyclass.create;
m.dodestroy;
end.
If you compile this with the heaptrace unit, it shows a leak. Delphi does not show a leak (you have to use another method to test this though, as heaptrace does not exist in Delphi - I used GetHeapStatus).
The cause is - without the self prefix on the destroy command, the Free Pascal compiler does not understand that you want to actually destroy a class. It still calls the destructor, but the class is not really destroyed.
Normally create and destroy are special methods - the compiler generates extra code to allocate the class and destroy it. Here the compiler does not generate the extra destroy code!
It seems Delphi always adds the self prefix automatically here. Free Pascal should too, in Delphi mode... I'd like it if this were fixed.
The fix is ofcourse to always prefix things with self if you want to refer to the current class rather than ommitting it.
Florian said it's not a bug - but I say the behaviour should still be changed because Delphi does not do this - and it even happens in freepascal when you use the -Sd (compile in Delphi mode) option.