Re: Destructors are useless?




Michi Henning wrote:
> 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.

Ok -- so how about this:

class someclass
{
~someclass()
{
try
{
System.Diagnostics.Assert(_myMemVar == null);
}
catch(ObjectDisposedException)
{
// you can't do much here anyway
}
}
}

.



Relevant Pages

  • Re: Destructors are useless?
    ... On process exit, Java ... >that destructors (or finalizers, if you prefer) will *not* run. ... >assertions are checked whenever the GC decides to garbage collect an object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destructors are useless?
    ... That leaves destructors completely useless. ... > for making sure any unmanaged resources are properly released. ... I'd like to write assertions about my own member ... It is interesting that Java doesn't have this problem. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destructor: not gauranteed to be called?
    ... >between finalizers and destructors, and you expected something of the ... more explanation, but that is the ENTIRE explanation. ... > expectations were justified. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Destructor: not gauranteed to be called?
    ... > implementation detail for destructors in C++. ... Destruction is a language ... Resource automatic freeing (mainly memory), ... The problem is that most developper know about finalizers (which ...
    (microsoft.public.dotnet.languages.vc)
  • Re: finalize() not guaranteed to be called -- ever
    ... >> problem is that objects in variables have value semantics and not ... is also the justification for not including this possibility in Java. ... Just adding destructors ... You suggest that the JVM can just guarantee that finalize() is called, ...
    (comp.lang.java.programmer)