[fpc-pascal] methods of an object to create others objects
Andrew Brunner
andrew.t.brunner at gmail.com
Tue Jul 6 00:39:15 CEST 2010
If Memory allocation is the point of discussion and you need to create
objects ...
You should nest the creations in order of usage.
In your declarations you needed bot Object1 and Object2 as TMyObject;
That said the proper usage would be:
obj1:=TMyObject.Create;
Try
obj2:=TMyObject.Create;
Try
Obj1.DoSomething1;
Obj2.DoSomething2;
Finally
FreeAndNil(Obj2);
end;
Finally
FreeAndNil(Obj1);
end;
I would like to know what the purpose is for having an auxiliary
constructor if they both reference createObj.
Anything other than nested Exception Handling is very poor practice.
On Mon, Jul 5, 2010 at 1:48 PM, Marcos Douglas <md at delfire.net> wrote:
> We can to use methods of an object to create others objects, or this
> is a bad programming practice?
> eg:
> var
> obj1, obj2: TmyObject;
> begin
> obj1 := tobj1.create; //ok
> obj2 := obj1.createObj; // is this recommended?
> try
> //...
> finally
> obj1.free;
> obj2.free;
> end;
> end;
>
> Why I ask this:
> If not exists the variable obj2 in call obj1.createObj will happen a
> memory leak, right? Because there is not variable to release.
>
>
> Marcos Douglas
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list