Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Flávio Etrusco
On Sat, Mar 2, 2013 at 7:51 AM, Michael Van Canneyt wrote: > > > On Sat, 2 Mar 2013, Howard Page-Clark wrote: > >> On 02/03/13 5:45, Flávio Etrusco wrote: >>> >>> On Fri, Mar 1, 2013 at 11:49 PM, Xiangrong Fang wrote: (...) > > Also, setting Self to nil is nonsense. > > If you do FreeAndNil(

Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Hans-Peter Diettrich
Sven Barth schrieb: If you want to ensure that MyInstance is Nil you need to do it like this: === code begin === try MyInstance := TMyClass.Create('AnNonExistentFile'); except MyInstance := Nil; end; === code end === When an exception occurs in Create, the assignment will never take pl

Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Howard Page-Clark
On 02/03/13 10:51, Michael Van Canneyt wrote: This will definitely cause a memory leak, because the FPC code does not know that an exception occurred in the constructor (you catch it with Except), and hence will not call the destructor. Thanks for the correction and explanation. Howard -- _

Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Michael Van Canneyt
On Sat, 2 Mar 2013, Howard Page-Clark wrote: On 02/03/13 5:45, Flávio Etrusco wrote: On Fri, Mar 1, 2013 at 11:49 PM, Xiangrong Fang wrote: Hi there, If my class constructor looks like this: constructor TMyClass.Create(fn: string); begin sl := TStringList.Create; try fs := TFil

Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Howard Page-Clark
On 02/03/13 5:45, Flávio Etrusco wrote: On Fri, Mar 1, 2013 at 11:49 PM, Xiangrong Fang wrote: Hi there, If my class constructor looks like this: constructor TMyClass.Create(fn: string); begin sl := TStringList.Create; try fs := TFileStream.Create(fn, fmOpenRead); except se

Re: [Lazarus] exception handling in constructor

2013-03-02 Thread Sven Barth
On 02.03.2013 03:49, Xiangrong Fang wrote: Hi there, If my class constructor looks like this: constructor TMyClass.Create(fn: string); begin sl := TStringList.Create; try fs := TFileStream.Create(fn, fmOpenRead); except self.Destroy; end; end; No, this is a bad idea. If

Re: [Lazarus] exception handling in constructor

2013-03-01 Thread Flávio Etrusco
On Fri, Mar 1, 2013 at 11:49 PM, Xiangrong Fang wrote: > Hi there, > > If my class constructor looks like this: > > constructor TMyClass.Create(fn: string); > begin > sl := TStringList.Create; > try > fs := TFileStream.Create(fn, fmOpenRead); > except > self.Destroy; > end; > end;

[Lazarus] exception handling in constructor

2013-03-01 Thread Xiangrong Fang
Hi there, If my class constructor looks like this: constructor TMyClass.Create(fn: string); begin sl := TStringList.Create; try fs := TFileStream.Create(fn, fmOpenRead); except self.Destroy; end; end; I create the objec like: MyInstance := TMyClass.Create('AnNonExistentFile');