object deletion order question

howard at eegsoftware.com howard at eegsoftware.com
Wed Dec 20 19:15:30 EST 2000


On Wed, 20 Dec 2000 19:45:19 GMT, "Fredrik Lundh" <fredrik at effbot.org>
wrote:

>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.
I neglected to mention that this code worked fine in the Python 1.5.2
version I was using but failed in the 2.0 version.

I guess my question was at termination time.  I had assumed that,
subject to cyclical references, all objects would be 'deleted' upon
interpreter termination.  Otherwise, there may have been a user object
that did file manipulation on deletion/closing which would never be
performed.  Say it aint so.

>> 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)

Actually, I thought I DID do most of that.  The CDX interface is built
with SWIG.  Cursory examination DOES show what I believe is reasonably
correct reference counting behavior.  All the deletions were
implicitly done when the interpreter terminated.  

>-- 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.

This is what I did in the Python wrapper code.

My intention is to fix it in the CDX libraries so that I can use the
'same' libraries in Python or C++ programs and not have to remember it
all the time.

Howard Lightstone
EEGSoftware
howard at eegsoftware.com



More information about the Python-list mailing list