Hi all,
I came across the need to do this and the Delphi help file warns against it if you're doing it within a component's event handler. I'm not sure of the specifics of why this is, and so I wanted to get some feedback on whether I can safely do this sort of thing in a general sense. Some code:
I can't see why I shouldn't be able to do this - but the help file has scared me a little. Obviously - I could leave out the call to Free in the MyObject method, and instead do this:
But, in this instance I will always be needing the object freed immediately after a call to DoSomething. Can I call Free within the MyObject method?
I came across the need to do this and the Delphi help file warns against it if you're doing it within a component's event handler. I'm not sure of the specifics of why this is, and so I wanted to get some feedback on whether I can safely do this sort of thing in a general sense. Some code:
Code:
type
TMyObject = class(TObject)
procedure DoSomethingAndFree;
end;
implementation
procedure TMyObject.DoSomethingAndFree;
begin
{ ... Do something here ... }
Free;
end;
var
a : TMyObject;
begin
a := TMyObject.Create;
a.DoSomethingAndFree;
end;
I can't see why I shouldn't be able to do this - but the help file has scared me a little. Obviously - I could leave out the call to Free in the MyObject method, and instead do this:
Code:
a := TMyObject.Create;
a.DoSomething;
a.Free;
But, in this instance I will always be needing the object freed immediately after a call to DoSomething. Can I call Free within the MyObject method?