3

Is there a way to remove all references to an object at once? I know that's unpythonic, so I'll explain what I'm trying to do and maybe someone knows a better way.

I'm writing an object-oriented wrapper around a SWIG wrapper for a C library. When a proxy for one of the C objects is deleted, it also deletes child objects (directly in C). I'd like that to also trigger deletion of their proxy objects in Python. Otherwise I run into a situation where there are Python objects carrying around invalid pointers which will segfault if they're accessed.

It looks sort of like this:

class Parent(object):
    def __init__(self):
        self.ptr = swig.createParent()
    def __del__(self):
        swig.deleteParent(self.ptr) # also deletes children

class Child(object):
    def __init__(self, parent):
        self.ptr = swig.createChild(parent)
    def __del__(self):
        swig.deleteChild(self.ptr)

And this is the situation I'm worried about:

p = Parent()
c = Child(parent)
del p
# accessing c.ptr now would be bad right?
jefdaj
  • 2,025
  • 2
  • 21
  • 33
  • 1
    possible duplicate of [How to delete every reference of an object in Python?](http://stackoverflow.com/questions/3013304/how-to-delete-every-reference-of-an-object-in-python) – agf Apr 09 '12 at 03:34
  • 1
    Probably duplicate question: http://stackoverflow.com/questions/3013304/how-to-delete-every-reference-of-an-object-in-python – Pinch Apr 09 '12 at 03:35
  • 1
    @Duc If you find a duplicate, and you don't have enough rep to vote to close, you can flag it as a duplicate as well as linking it (if it hasn't been done already :) – agf Apr 09 '12 at 03:36
  • 1
    This isn't a duplicate of those purely-Python weakref questions since here, deleting one wrapperized C object may cause other C objects to get deleted (and possibly free'd), directly in C. Take more care before voting to close. – smci Apr 09 '12 at 04:56

2 Answers2

3

If I understand you correctly, you are wrapping some C code, and the C code has a destructor that can be called. After that, any attempt to use the pointer to the C code object causes a fatal crash.

I am not sure of your exact situation, so I am going to give you two alternate answers.

0) If the C object can be freed for some reason out of your control, and you need to make sure your Python wrapper code doesn't crash, you need to make the Python wrapper know whether the C object is available or not. Make your Python object handle the pointer no longer being valid. You could raise a Python exception, return an error code, or just have the method functions become no-op functions, depending on what you are doing. The C object going away doesn't free the Python object, so you can handle this cleanly.

1) If the C object is only freed when the Python object is freed, you don't have a problem. Python references, when they go out of scope or you call del() on them, do not free the Python object; they just decrement the reference count on that object. When the reference count goes to zero, then the object is freed and your __del__() method function can call into the C code to free the C object.

You can watch how it works by running this code:

class DelTest(object):
    def __init__(self):
        print "__init__() called: object %08x created" % id(self)
    def __del__(self):
        print "__del__() called: object %08x destroyed" % id(self)

print "begin"
print "creating object, binding to name d"
d = DelTest()
print "adding reference bound to name x"
x = d
print "adding reference bound to lst[0]"
lst = []
lst.append(d)
print "deleting lst"
del(lst)
print "deleting x"
del(x)
print "deleting d"
del(d)
print "end"

Output from the above:

begin
creating object, binding to name d
__init__() called: object 01e4db50 created
adding reference bound to name x
adding reference bound to lst[0]
deleting lst
deleting x
deleting d
__del__() called: object 01e4db50 destroyed
end
steveha
  • 74,789
  • 21
  • 92
  • 117
  • I think in my case all I need to do is keep a reference to the Parent in each Child object. But this is definitely a good general solution. – jefdaj Apr 09 '12 at 04:03
0

A note about the behavior of __del__() method.

del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero.

Therefore even if you delete parent, it does not necessarily mean that __del__ is executed immediately until there are any references to it. Here is an example.

>>> class C(object):
...     def __del__(self):
...         print "deleting object of type: %s" %self.__class__
... 
>>> class D(object):
...     def __init__(self, parent):
...         self.parent = parent
...     def __del__(self):
...         print "deleting object of type: %s" % self.__class__
... 
>>> c = C()
>>> d = D(c)
>>> del c
>>> del d
deleting object of type: <class '__main__.D'>
deleting object of type: <class '__main__.C'>

Note that the __del__ method of C is called after the del d call.

Praveen Gollakota
  • 37,112
  • 11
  • 62
  • 61
  • 1
    I thought the garbage collector would fail in this case because c.ptr isn't a Python reference--it's a SWIG pointer. So p was the only reference to the Parent. But that brings up an obvious solution I didn't think of before: just keep a Python reference from Child-->Parent too. – jefdaj Apr 09 '12 at 03:54