Destructors and exceptions

David Turner dkturner at telkomsa.net
Wed Jun 9 03:29:44 EDT 2004


Hi

> When an exception is handled, you can access the stack traceback. This 
> contains references to all the local variables which were in scope at the 
> point when the exception was thrown. Normally these variables would be 
> destroyed when the functions return, but if there is a traceback 
> referencing them they stay alive as long as the traceback is accessible 
> which is usually until the next exception is thrown.

Then why not unreference the traceback (and therefore destroy it and
the down-stack locals, if the exception handler hasn't made another
reference) at the end of the handling suite?


> If you really want to be sure your objects are destroyed, then use 'del' in 
> the finally suite of a try..finally. This ensures that the traceback can't 
> be used to reference the objects that you deleted.

As I've pointed out elsewhere, this works but it doesn't address the
problem I'm trying to solve.  The problem is that it's easier for the
user of an object (who didn't create it, and probably hasn't read the
documentation) to omit the try/finally construct.  Therefore, this is
what he will tend to do.  I'm looking at what I, as a designer, can do
to prevent this from happening.

Regards
David Turner



More information about the Python-list mailing list