Destructor never called ???

Tim Roberts timr at
Thu Sep 12 01:04:23 EDT 2002

muk_pr at (vb) wrote:

>  I have the following code my problem is when I call any methods of  
>       TCVector_Object the destructor never executes.
>I mean
>v = module.CreateTCVector()
>del v;
>//destructor runs. message shown .OK
>v = module.CreateTCVector()
>del v;//or closing python interpreter
>destructor never gets called. Any ideas?

Yes.  Don't put critical code in a destructor.

When you delete "v", it removes the name "v" from your global namespace,
and decrements the reference count on your TCVector_Objet to 0.  That means
that no one is currently referencing the object.

However, Python is a garbage-collected system.  Unlike in COM, an object is
NOT destroyed as soon as its reference count goes to 0.  Your object will
only be destroyed if the garbage collector happens to run, and decides that
it might be a convenient time to actually free up the space your object

If the garbage collector doesn't run before Python terminates, there is no
NEED to clean up memory, so the destructor will NEVER run.

Do a google search for "deterministic destructor python"; you'll see the
previous threads on this.
- Tim Roberts, timr at
  Providenza & Boekelheide, Inc.

More information about the Python-list mailing list