Re: Destructors are useless?
- From: "Michi Henning" <michi@xxxxxxxxxxx>
- Date: Wed, 27 Apr 2005 23:03:25 +1000
"Tom Porterfield" <tpporter@xxxxxxxx> wrote in message
news:7tcxplo56alq.dlg@xxxxxxxxxxxxxxxxxxx
> On Wed, 27 Apr 2005 17:40:26 +1000, Michi Henning wrote:
>
> > Hmmm... That leaves destructors completely useless. There is nothing, not
even asserting, that I can do safely.
> > Of course, that begs the question: why have destructors when I can't do
anything with them? As far as I can
> > see, the only legal statements inside a destructor are effectively no-ops.
>
> If you are writing all managed code, then this statement is essentially it.
Yes, it sure looks like it.
> Within a pure managed environment this is no need to write a destructor.
> Destructors are useful for releasing unmanaged resources. The garbage
> collector has no specific knowledge on how to clean up an unmanaged
> resource. Your class that encapsulates an unmanaged resource does have
> that knowledge. So in your destructor you would take the responsibility
> for making sure any unmanaged resources are properly released. Let the GC
> handle your managed resources.
Sure, I understand all that. A destructor can't do anything that relies on
another
object. In other words, a destructor can't dereference anything, and it cannot
even call a static function. Fine, I accept that. So what are destructors good
for?
Releasing unmanaged resources is one thing. (If I can accept that unmanaged
resources may not be released promptly.) But the *one thing* I still would
like to use destructors for is to check whether my program is still in a sane
state. In other words, I'd like to write assertions about my own member
variables. Unfortunately, I can't even do that: if the assertion holds, no
problem;
but, if the assertion fails, the mere act of having it fail is sufficient to
cause
undefined behavior. So, damned if I do, and damned if don't: if I don't assert,
my program has a bug and will go and do undefined things. If I do assert, the
act
of detecting that my program has a bug causes my program to go and do
undefined things. Talk about being caught between a rock and a hard place...
It is interesting that Java doesn't have this problem. On process exit, Java
guarantees
that destructors (or finalizers, if you prefer) will *not* run. This means that
it's perfectly
safe to put assertions into Java finalizers: while the process is alive and
running, the
assertions are checked whenever the GC decides to garbage collect an object.
And, if the process is exiting, destructors don't run at all, meaning that
there are
no issues with respect to destruction order. Of course, that means that
assertions
don't run during process shut down, but at least my process gets to shut down
without crashing.
And, of course, the idea that C# destructors are good for releasing unmanaged
resources seems flawed, too. After all, how many realistic programs are there
that can release unmanged resources without having to enrol the aid of some
other
managed resource? For example, in my particular case, the program crashed
because
of a destructor that looked like this:
class SomeClass
{
~SomeClass()
{
if(_destroyed)
{
_logger.warning("Forgot to destroy SomeClass");
}
}
private Logger _logger;
}
What I'm suggesting here is that, for realistic programs, chances are pretty
slim
that a destructor can clean up an unmanaged resource without having to ask for
help from some managed object. But, of course, as soon as that is the case,
the destructor cannot be used anymore. I'm left to conclude that destructors
are so useless that I can't even use them to make assertions about my own
member variables, let alone use them to reclaim unmanaged resources.
It truly would seem appropriate to remove destructors from the language
altogether. At least, that way, people wouldn't have their illusions shattered.
Of course, the alternative would be to not run destructors on process shutdown,
like Java does, and the problem would go away...
Cheers,
Michi.
--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com
.
- Follow-Ups:
- Re: Destructors are useless?
- From: Mattias Sjögren
- Re: Destructors are useless?
- From: Dilip
- Re: Destructors are useless?
- From: Daniel Jin
- Re: Destructors are useless?
- References:
- Destructors are useless?
- From: Michi Henning
- Re: Destructors are useless?
- From: Tom Porterfield
- Destructors are useless?
- Prev by Date: Week numbers totally incorrect !!
- Next by Date: Re: Constants Array of "Structs"
- Previous by thread: Re: Destructors are useless?
- Next by thread: Re: Destructors are useless?
- Index(es):
Relevant Pages
|