On 05/07/2010 21:03, Marcos Douglas wrote:
I use this technique a lot, but thanks :) Unfortunately, this is not
the *problem*.
The ask is:
If a function creates, inside, a object and there is no variable to
receive this pointer, will happen a memory leak?
In my eg before, this is Okay:
obj2 := obj.createObj;
The obj2 is a pointer to release.
But, like that:
obj1.createObj; //no variable to receive
...now, where is the pointer to release?
That would be, if a crash happens inside obj1.createObj? Because
otherwise the obj is stored in obj2.
If it happens in there, then handle it in there => there is result as
variable.
function TMyObject.creatObj : TSomeObj;
begin
result := TsomeObject.create;
end;
function TMyObject.creatObj : TSomeObj;
begin
result := TsomeObject.create;
try
DoFooBar;
except
result.Free; // will not be used by caller, but be aware it has
still a value, which now is invalid
end;
end;
In the line
result := TsomeObject.create;
the only crash that can happen is inside "create" => a crash in the
constructor is handled by the compiler, the object will be freed.
Martin
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal