Search code examples
pythongarbage-collectioncpython

Python object has no referrers but still accessible via a weakref?


Should it be possible for gc.get_referrers(obj) to return an empty list for an object, but the object still be accessible through a weak reference?

If so how would I start trying to identify the cause for this object not being garbage collected?

Edit: I'm not sure exactly how a code sample would help in this case - there's obviously a strong reference somewhere, but I'll be damned if I can find it. I was of the impression that all strong references to an object would be identified by get_referrers().

Edit: Solved. I found the variable with a strong reference - It was inside the game event loop but wasn't a class variable so get_referrers wasn't picking it up.


Solution

  • Yes: http://docs.python.org/library/weakref.html

    A weak reference won't be keeping the object alive.

    The get_referrers() function will only locate those containers which support garbage collection; extension types which do refer to other objects but do not support garbage collection will not be found.

    What makes you think the object isn't getting collected? Also, have you tried gc.collect()?