destructors, how?

Bryce Kampjes bryce at kampjes.demon.co.uk
Tue Oct 22 16:48:27 UTC 2002


Ragnar Hojland Espinosa writes:
 > On Tue, Oct 22, 2002 at 03:55:39PM +0100, Peter Crowther wrote:
 > > > I can't find out how to write a destructor 
 > > > or something functionally equivalent.
 > > 
 > > Check out implementors of 'finalize' (IIRC).  In essence, you hold onto
 > > the object using a weak array; are notified when it is
 > > garbage-collected; and clear appropriate fields based on copies of the
 > > data in the object.
 > 
 > Finalize did get some nice matches.  Does finalize get called right away, or
 > only when squeak does the GC?  And if so, am I guaranteed that it'll be GCed
 > that moment?
 > 
 > The thing is.. I'm leaving behind lots open postgres connections which would
 > be nicely closed with a "destructor", and right now I don't know how to
 > automatically close (maybe you cant?) when it should (the sooner the
 > better). Obviously, postgres complains quite loudly when you have too many
 > open, and things suddely get very ugly.

You might want to try using blocks e.g.:
connectionPooler withConnection: 
    [:conn| the code that uses the connection "conn"].

This way the method withConnection: can both open the connection
and close it. This is a fairly standard idiom to wrap
creation and deletion of a resource.

I'm guessing you will have to write your own connection 
pooler but this will give you limited scope access to that 
connection. It can excape (but that could happen via a 
pointer in C++ as well).

Bryce



More information about the Squeak-dev mailing list