object deletion order question

Fredrik Lundh fredrik at effbot.org
Wed Dec 20 14:45:19 EST 2000

howard at eegsoftware.com wrote:
> However, that led my to question:
>         What ordering does Python use for object deletions?

any order it likes.  it may remove an object as soon as
possible, or never at all.

> When Python terminated (or at other deletions), it called the
> delete method of the SoundBuffer  objects after deleting the
> Sound objects, leaving DirectX calling into unknown data space
> (I know..it's a Microsoft tradition).

Direct X uses reference counting just like Python.  if you mess
up the reference count in Python, it'll crash as well.

avoiding this is easy, of course.  just make sure you keep the
reference counts in sync:

-- use Python wrappers for all reference-counted Direct X
   objects (e.g. SoundObject, SoundBufferObject)

-- only access Direct X objects via the Python wrappers
   (e.g. through an LPDIRECTSOUND member in the Sound
   object class)

-- add a PyObject (or better, SoundObject) pointer to the
   SoundBuffer wrapper class, and make it point to the Sound
   object when you create the buffer (don't forget to call
   Py_INCREF on it)

-- only destroy Direct X objects in the Python wrapper's
   dealloc method.  in the SoundBuffer wrapper, make sure
   you decrement the pointer to the Sound object.

if you do this, the sound object will never be destroyed before
any of its sound buffers.

> Of course, I realize (now) I should not have *assumed* ANY
> deletion order.

why are you asking when you've already figured it out ;-)


More information about the Python-list mailing list