Page 1 of 1

Issues with __del__

Posted: Sun Jul 15, 2012 8:45 pm
by Omega_K2
__del__ may cause that objects are not deleted from memory (unless it's manually taken care of :P):
http://docs.python.org/reference/datamodel.html#object.__del__
http://docs.python.org/library/gc.html#gc.garbage

Edit: Also, it may be possible that __del__ is never called because of this problem:
http://bugs.python.org/issue9072
/Edit


So I suggest that the __del__ in decorator.py is actually removed and that the events are unregistered else where :P

Posted: Sun Jul 15, 2012 9:10 pm
by satoon101
This doesn't seem as big a deal right now as you are trying to lead us to believe. If the object actually isn't removed from memory, that really isn't the fault of using __del__ to unregister for an event. If we don't call __del__, the object will still remain in memory anyway, and the event will not be unregistered. So, what do we accomplish in this regard by changing our method of unregistering for the event?

I can see that if an error occurs during __del__ (which is highly unlikely), there would be the issue of the event not being unregistered. However, the only things "we" do in __del__ is unregister for the event and then re-call __del__ (this change hasn't been pushed yet).

Satoon

Posted: Tue Jul 17, 2012 12:06 am
by Omega_K2
The problem with using __del__ (and not just when using the decorator) is that it may lead to unexpected memory leaks. As such, it should be avoided to use, just because it seems like something dangerous to use (and so far I've been able to find a way to get things working properly without using the __del__ method even if it requires some extra work).
This is probably also a candiate for the 'SP design docs' (in addition to the PEP8 stuff).

On a side note that recent change actually contains an error since object does not have a __del__ method (no python class has a __del__ method by default AFAIK):

Code: Select all

>>> object.__del__
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    object.__del__
AttributeError: type object 'object' has no attribute '__del__'
>>> class a(object):
   def __del__(self):
      super(a, self).__del__()

      
>>> b = a()
>>> del b
Exception AttributeError: "'super' object has no attribute '__del__'" in <bound method a.__del__ of <__main__.a object at 0x02AE0770>> ignored

Posted: Tue Jul 17, 2012 12:15 am
by satoon101
Good point about no native method, don't know why I didn't remember that. I already figured out how to do this without using __del__, and those changes are forthcoming. I had been working on a few other things, but I will hold of on those and update this right now.

Satoon