[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]question about Finalize in C#

From: S11001001
Subject: Re: [DotGNU]question about Finalize in C#
Date: Sun, 08 Sep 2002 15:40:02 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1b) Gecko/20020904

Juhani Honkala wrote:
You should never depend on a finalizers to release any criticial resources. Instead of writing finalizer write an explicit release
method like Dispose() and use Finalize() only as a safety net or last
resort (in case user forgets to call Dispose()). Remember to call
superclass Finalize() if you override Finalize(). (If my memory serves
me right there's an interface in C# called IDispose that's tries to
somehow standardize use of explicit destruction, i'm not 100% sure tho)

The finalizers aren't so useful in C# (nor Java) because so many resources are "managed", i.e., file descriptors, memory, whatnot. Those implementing the IDisposable interface (which abstracts Dispose(bool), not Dispose()) can use the using (MyObject declaration_var) {} to call Dispose at the end of the last bracket, IIRC (though I don't remember whether it calls Dispose(true) or Dispose(false)).

The difference between the args is that when you pass true, it also "releases" managed resources; how I write it when it comes up is to set all Objects (i.e., pointers) to null, because it doesn't say whether to go recursive to instance vars in what I've read (BCL std), and anyway, they may still be in use elsewhere.

Stephen Compall
DotGNU `Contributor' --

There are many people who say, "Well, first let's invite people to
join the community, and then let's teach them about freedom." And that
could be a reasonable strategy, but what we have is almost everybody's
inviting people to join the community, and hardly anybody's teaching
them about freedom once they come in...If you think talking about
software freedom is a good strategy, please join in doing step
two. There are plenty working on step one.
        -- RMS, on the Free Software/`Open Source' community

reply via email to

[Prev in Thread] Current Thread [Next in Thread]