[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [DotGNU]question about Finalize in C#
From: |
James Mc Parlane |
Subject: |
RE: [DotGNU]question about Finalize in C# |
Date: |
Tue, 10 Sep 2002 12:47:39 +1000 |
IMHO - If you have to use a finaliser, then why even bother to use a GC
based system. Just code in C or C++ :)
> -----Original Message-----
> From: Juhani Honkala [mailto:address@hidden
> Sent: Thursday, September 05, 2002 6:23 AM
> To: Ian Fung
> Cc: address@hidden
> Subject: Re: [DotGNU]question about Finalize in C#
>
>
> On Wed, 4 Sep 2002, Ian Fung wrote:
>
> > C# Q: I understand what Finalize does. but from what i
> understand, it
> > is not guarentteed to be called (ie only when the
> GC is called) and
> > it also takes two calls to teh GC to retrieve the
> object. what kind
> > of cleanup would you want to put in Finalize? i
> mean for example, if
> > i open a file, wouldnt i want to close it outside
> of Finalize to
> > make sure it closes everytime instead of putting
> it in Finalize?
> >
>
> C# (unfortunately) lacks of explicit destruction scheme as in C++.
>
> 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)
>
> i'm not sure how c# syntax do but in Java it would something like
>
> public void dispose() {
> //release all critical resources here
> }
>
> protected void finalize() {
> try {
> dispose();
> } finally {
> super.finalize()
> }
> }
>
>
> Generally you should avoid using only finalizers to free resources,
> in some situations it might be acceptable like in freeing non-critical
> native resources or something but these cases should be considered as
> exception.
>
>
>
> if things are different in C# than in Java, feel free to correct
> me ;)
>
> juhani
>
> _______________________________________________
> Developers mailing list
> address@hidden
> http://www.dotgnu.org/mailman/listinfo/developers
>