Issues with __del__

Discuss API design here.
Omega_K2
Senior Member
Posts: 227
Joined: Sat Jul 07, 2012 3:05 am
Location: Europe
Contact:

Issues with __del__

Postby Omega_K2 » Sun Jul 15, 2012 8:45 pm

__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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Jul 15, 2012 9:10 pm

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
Omega_K2
Senior Member
Posts: 227
Joined: Sat Jul 07, 2012 3:05 am
Location: Europe
Contact:

Postby Omega_K2 » Tue Jul 17, 2012 12:06 am

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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Jul 17, 2012 12:15 am

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

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 13 guests