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

2010-07-06 Thread spir
On Mon, 5 Jul 2010 16:51:54 -0300 Marcos Douglas m...@delfire.net wrote: 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. Do you mean using a function as a statement? (for its so-called

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

2010-07-06 Thread Graeme Geldenhuys
On 5 July 2010 23:17, Michael Van Canneyt wrote: I would even add to this that you need to guard for exceptions: A:=TSomeClass.Create; try  // do stuff finally  A.Free; Make sure it is freed, even in case of exception. end; Wouldn't it be nice if we had a try..except..finally statement

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

2010-07-06 Thread Marco van de Voort
In our previous episode, Graeme Geldenhuys said: Wouldn't it be nice... Ah, the kiss-of-death phrase for a new feature request :-) ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal

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

2010-07-06 Thread Michael Van Canneyt
On Tue, 6 Jul 2010, Graeme Geldenhuys wrote: On 5 July 2010 23:17, Michael Van Canneyt wrote: I would even add to this that you need to guard for exceptions: A:=TSomeClass.Create; try  // do stuff finally  A.Free; Make sure it is freed, even in case of exception. end; Wouldn't it be

Re: [fpc-pascal] Converting a graphical DOS program to fpc

2010-07-06 Thread Nikolay Nikolov
On 07/01/2010 05:46 AM, Simon Webster wrote: Dear Free Pascallers, I'm wanting to convert a Turbo Pascal program to fpc, to run (ultimately) under linux, although I'm actually developing on a mac, and using virtualbox to run ubuntu. The program in question is a relatively large non OOP program

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

2010-07-06 Thread Marcos Douglas
On Mon, Jul 5, 2010 at 6:06 PM, Graeme Geldenhuys graemeg.li...@gmail.com wrote: On 5 July 2010 22:03, Marcos Douglas wrote: The ask is: If a function creates, inside, a object and there is no variable to receive this pointer, will happen a memory leak? Yes you will have a memory leak. Easy

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

2010-07-06 Thread Marcos Douglas
On Mon, Jul 5, 2010 at 9:07 PM, Reimar Grabowski reimg...@web.de wrote: On Mon, 5 Jul 2010 16:51:54 -0300 Marcos Douglas m...@delfire.net wrote: What do you mean a function has effects is already bad? A function/procedure always has effects, don't? IMHO he means that a function should only

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

2010-07-06 Thread Andrew Brunner
obj1:=TMyObject.Create; Try  obj2:=TMyObject.Create;  Try    Obj1.DoSomething1;    Obj2.DoSomething2;   Finally     FreeAndNil(Obj2);   end;  Finally   FreeAndNil(Obj1); end; Hum... I do not agree. Why not this? See.. obj1:=TMyObject.Create; obj2:=TMyObject.Create; Try  

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 3:05 AM, spir denis.s...@gmail.com wrote: The object Parser creates obj through the line string. If the *line* is broke, Parser do not can create an object and an Exception is created; the *obj* never is nil because there are exception handling. I do not see the

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 9:22 AM, Michael Van Canneyt mich...@freepascal.org wrote: On Tue, 6 Jul 2010, Graeme Geldenhuys wrote: On 5 July 2010 23:17, Michael Van Canneyt wrote: I would even add to this that you need to guard for exceptions: A:=TSomeClass.Create; try  // do stuff finally

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 12:10 PM, Andrew Brunner andrew.t.brun...@gmail.com wrote: Hum... I do not agree. Why not this? See.. obj1:=TMyObject.Create; obj2:=TMyObject.Create; Try   Obj1.DoSomething1;   Obj2.DoSomething2; Finally  FreeAndNil(Obj1);  FreeAndNil(Obj2); end; In your

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

2010-07-06 Thread Luiz Americo Pereira Camara
Marcos Douglas escreveu: Better: obj1 := nil; obj2 := nil; Try obj1 := TMyObject.Create; obj2 := TMyObject.Create; obj1.DoSomething1; obj2.DoSomething2; finally obj1.Free; obj2.Free; end; The objectcs are protected. But is boring... :) Everybody codify like that, afraid if

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

2010-07-06 Thread Andrew Brunner
On Tue, Jul 6, 2010 at 10:31 AM, Marcos Douglas m...@delfire.net wrote: Better: obj1 := nil; obj2 := nil; Try  obj1 := TMyObject.Create;  obj2 := TMyObject.Create;  obj1.DoSomething1;  obj2.DoSomething2; finally  obj1.Free;  obj2.Free; end; The objectcs are protected. But is

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

2010-07-06 Thread Marco van de Voort
In our previous episode, Andrew Brunner said: obj1 := nil; obj2 := nil; Try ?obj1 := TMyObject.Create; ?obj2 := TMyObject.Create; ?obj1.DoSomething1; ?obj2.DoSomething2; finally ?obj1.Free; ?obj2.Free; end; The objectcs are protected. But is boring... :) Everybody

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

2010-07-06 Thread Andrew Brunner
On Tue, Jul 6, 2010 at 11:39 AM, Marco van de Voort mar...@stack.nl wrote: In our previous episode, Andrew Brunner said: obj1 := nil; obj2 := nil; Try ?obj1 := TMyObject.Create; ?obj2 := TMyObject.Create; ?obj1.DoSomething1; ?obj2.DoSomething2; finally ?obj1.Free;

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

2010-07-06 Thread Jonas Maebe
On 06 Jul 2010, at 18:42, Andrew Brunner wrote: Double Nope. You cannot access methods of a nil object. You can, as long as such methods are not virtual (and free is not a virtual method). Nil.Free will in-it-and-of-it cause a read access violation. This is what free does: if

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 2:04 PM, Reimar Grabowski reimg...@web.de wrote: On Tue, 6 Jul 2010 12:05:36 -0300 Marcos Douglas m...@delfire.net wrote: We talk about just *functions* or we talk about *methods* too? It is meant for 'method functions', too. But I don't follow that rule strictly and

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

2010-07-06 Thread Jonas Maebe
On 06 Jul 2010, at 19:51, Marcos Douglas wrote: Can an exception happen in Free method? It can happen in Destroy, which is called by Free. Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 2:55 PM, Jonas Maebe jonas.ma...@elis.ugent.be wrote: On 06 Jul 2010, at 19:51, Marcos Douglas wrote: Can an exception happen in Free method? It can happen in Destroy, which is called by Free. And do not have medicine for that, right? Marcos Douglas

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

2010-07-06 Thread Jonas Maebe
On 06 Jul 2010, at 20:06, Marcos Douglas wrote: On Tue, Jul 6, 2010 at 2:55 PM, Jonas Maebe jonas.ma...@elis.ugent.be wrote: On 06 Jul 2010, at 19:51, Marcos Douglas wrote: Can an exception happen in Free method? It can happen in Destroy, which is called by Free. And do not have

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

2010-07-06 Thread Reimar Grabowski
On Tue, 6 Jul 2010 14:54:29 -0300 Marcos Douglas m...@delfire.net wrote: Like obj1.CreateObj() ? =) Sounds a little generic for my liking, but at least you can guess that there is something more going on than just returning a value, so, yes. :) I'd rather use something like obj2 :=

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 4:09 PM, Reimar Grabowski reimg...@web.de wrote: On Tue, 6 Jul 2010 14:54:29 -0300 Marcos Douglas m...@delfire.net wrote: Like obj1.CreateObj() ? =) Sounds a little generic for my liking, but at least you can guess that there is something more going on than just

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

2010-07-06 Thread Graeme Geldenhuys
On 6 July 2010 18:42, Andrew Brunner andrew.t.brun...@gmail.com wrote: Nope. If Obj2 failed to create you will have a problem with Obj2.Free. Nope. That's why it is free and not destroy. Double Nope.  You cannot access methods of a nil object.  Nil.Free will in-it-and-of-it cause a read

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

2010-07-06 Thread Graeme Geldenhuys
On 6 July 2010 19:51, Marcos Douglas m...@delfire.net wrote: That's the problematic one I think. Can an exception happen in Free method?!  :-O Yes! I often have to debug those in our complex business objects. -- Regards, - Graeme - ___ fpGUI -

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

2010-07-06 Thread Andrew Brunner
On Tue, Jul 6, 2010 at 5:04 PM, Graeme Geldenhuys graemeg.li...@gmail.com wrote: I meant is as in it is safe to call MyObj.Free even if MyObj = nil. I answered based on the explicit question. But yes, in your example where MyObj was an instance, the code in the destructor could still trip

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

2010-07-06 Thread Alberto Narduzzi
Andrew Brunner wrote: It most certainly is not safe. LOL. Free calls destroy.. Destroy may contain other frees and routines... You cannot guarantee that free will even return. the point was to call .Free on an object that might not have been initialized, not what Free ( or destroy, for the

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

2010-07-06 Thread Graeme Geldenhuys
On 7 July 2010 00:04, Andrew Brunner wrote: You got that right.  Which is my point exactly.  If free blows out w/o a nested exception handle to catch it... The entire object goes into limbo and will never be free and thus cause a memory leak. Even with a try..except, if you call MyObj.Free

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

2010-07-06 Thread Andrew Brunner
On Tue, Jul 6, 2010 at 5:09 PM, Graeme Geldenhuys graemeg.li...@gmail.com wrote: Even with a try..except, if you call MyObj.Free and it causes an exception, you are screwed either way and it will always cause a memory leak. Best is no notify the user some how, or crash out of the program, or

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

2010-07-06 Thread Graeme Geldenhuys
On 7 July 2010 00:09, Andrew Brunner wrote: Whew,  Ok.  Years back Delphi (the experience I'm drawing on) Object.Free could not be done.  Now that I know FPC does the check for nil I probably won't be using FreeAndNil anymore.  But this raises a large concern for me.  That is the EXTRA if

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

2010-07-06 Thread Reimar Grabowski
On Tue, 6 Jul 2010 17:14:38 -0300 Marcos Douglas m...@delfire.net wrote: Well... I think use a name as CreateSpecialTMyObjectInstance() is very especific... but, as you said, in the end it is all about personal style. Perhaps I have not made myself clear. example (makes not much sense; just to

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

2010-07-06 Thread Marcos Douglas
On Tue, Jul 6, 2010 at 7:14 PM, Andrew Brunner andrew.t.brun...@gmail.com wrote: On Tue, Jul 6, 2010 at 5:09 PM, Graeme Geldenhuys graemeg.li...@gmail.com wrote: Even with a try..except, if you call MyObj.Free and it causes an exception, you are screwed either way and it will always cause a