Re: Do garbage collected languages need destructors?

From: Robert Klemme (bob.news_at_gmx.net)
Date: 10/30/03


Date: Thu, 30 Oct 2003 15:53:39 +0100


"Dave Harris" <brangdon@cix.co.uk> schrieb im Newsbeitrag
news:memo.20031029201839.1824A@brangdon.m...
> ulf.wendel@phpdoc.de (Ulf Wendel) wrote (abridged):
> > Do garbage collected languages need destructors?
>
> Depends on what you mean by that. Generally we all need to model object
> lifetimes and we all need deterministic release of resources.
Destructors
> are a C++ mechanism for dealing with that. If you solve the problem a
> different way, for example by using block closures or "around methods",
> then you don't need destructors.
>
> In practice few GCed languages have them. I gather .NET is getting
> destructors, but that's the exception rather than the rule. Note that
> finalisers play a different role, because they are not executed
> deterministically and because they can't really release resources. (That
> is, an object may become reachable after its finaliser has been run, so
a
> finaliser ought to preserve its class invariant, and in general the
class
> invariant may require resources.)
>
> Java is unusual in having neither block closures nor destructors. This
> makes object lifetimes difficult to model in that language. It certainly
> "needs" something.

Well, at least it has "finally":

public void executeSql( String sql ) throws SQLException {
  Connection tx = allocateTx();

  try {
    Statement st = tx.createStatement();

    try {
      st.execute( sql );
    }
    finally {
      st.close();
    }
  }
  finally {
    deallocateTx( tx );
  }
}

Not impossible - but ugly.

Cheers

    robert



Relevant Pages