[fpc-pascal] methods of an object to create others objects

Marcos Douglas md at delfire.net
Mon Jul 5 22:26:52 CEST 2010


On Mon, Jul 5, 2010 at 5:14 PM, Martin <fpc at mfriebe.de> wrote:
>
> 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.

Thanks, but lets continue  :)

In another words, get you method:

> function TMyObject.creatObj : TSomeObj;
> begin
>  result := TsomeObject.create;
> end;

If I have a variable myObj, I can do this:
someObj := myObj.createObj;

This is the same:
someObj := TsomeObject.create;

Rigth?

But if a remove the variable *someObj* and call like this:
myObj.createObj; // I _removed_ the *someObj* here!!!

What will happen with the "result" variable? Is it released by the compiler?


Marcos Douglas



More information about the fpc-pascal mailing list