[Mono-list] constructor and new object

John Barnette jbarn@httcb.net
Fri, 9 Nov 2001 18:20:16 -0600


Lloyd,

Isn't this a situation tailor-made for a finally block?


~ j.

----- Original Message ----- 
From: "Lloyd Dupont" <lloyd@galador.net>
To: <mono-list@ximian.com>
Sent: Friday, November 09, 2001 5:34 PM
Subject: [Mono-list] constructor and new object


> imagine i have an object with native resource which should explicitly be 
> deallocated.
> imagine that in my constructor i alloc the native resource and throw an 
> error, will my "destructor" be called or the native resource never be 
> freed ?
> 
> exemple which of these 2 code is correct ?
> -----------------------------
> public class Handler
> {
> GCHandle handle;
> public Handler(object o) {
> handle = GCHandle.Alloc(o);
> throw new SystemException("just for fun");
> }
> public ~Handler() {
> handle.Free();
> }
> }
> ------------------------------
> public class Handler
> {
> GCHandle handle;
> public Handler(object o) {
> handle = GCHandle.Alloc(o);
> try { throw new SystemException("just for fun"); }
> catch {
> handle.Free();
> throw;
> }
> }
> public ~Handler() {
> handle.Free();
> }
> }
> ------------------------------
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>