[Mono-list] constructor and new object
A Rafael D Teixeira
rafaelteixeirabr@hotmail.com
Sat, 10 Nov 2001 15:54:15 -0200
In truth not in that place: A finally block always execute...
One of the things Lloyd needs is that when an exception occur inside the
constructor the resource must be freed. BUT SEE BELOW.
Lloyd donīt forget: destructors donīt exist on .NET, they are in truth
finalizers that get called by the GC when it sees fit, not when the object
exits scope.
The recomendation is to provide a explicit 'dispose' or 'close' method, that
the the objectīs client can call when done. Example:
------------------------------
public class Handler
{
GCHandle handle;
public Handler(object o) {
handle = GCHandle.Alloc(o);
try {
throw new SystemException("just for fun"); }
catch {
handle.Free(); // if the object isnīt even get constructed
// free the resource
throw;
}
}
public void Dispose() // method for caller to
{
handle.Free();
}
}
class HandlerClient
{
public void UseIt()
{
Handler h = new Handler(this);
try {
... do something interesting ... that may raise exceptions
... the exceptions will be throw to an enclosing try block
... but the Dispose() method will called always if the object
... gets constructed in the first place
}
finally {
if (h != null)
h.Dispose();
}
}
}
Microsoft even concocted an specific syntax: the "using" clause.
For it to work implement IDisposable on Handler (the same Dispose method
above) and then have:
class HandlerClient
{
public void UseIt()
{
using (Handler h = new Handler(this))
{
... do something interesting ... that may raise exceptions
... the exceptions will be throw to an enclosing try block
... but the Dispose() method will called allways if the object
... gets constructed in the first place
}
}
}
Rafael Teixeira
Brazilian Developer
>From: "John Barnette" <jbarn@httcb.net>
>To: "Lloyd Dupont" <lloyd@galador.net>, <mono-list@ximian.com>
>Subject: Re: [Mono-list] constructor and new object
>Date: 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
> >
>
>
>_______________________________________________
>Mono-list maillist - Mono-list@ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-list
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp