[Mono-list] constructor and new object
Lloyd Dupont
lloyd@galador.net
Sat, 10 Nov 2001 00:34:09 +0100
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();
}
}
------------------------------