gc.get_objects()

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Sep 17 10:16:13 EDT 2012


On Mon, 17 Sep 2012 14:42:56 +0200, Matteo Boscolo wrote:

> Hi All,
> 
> I'm facing some trouble with a win32com application, it seems, that some
> com object (win32com) are still in the gc.get_objetc() list, even if I
> set to non the objetc and I'm out of the scope of that objects.

You can't set an object to None. An object is itself. It is *always* 
itself, you can't tell Python to turn it into None.

What you can do is re-bind a *name* from an object to None.

Suppose you say:

x = Spam()

then there is a binding between name "x" and the object Spam(), which I 
will call "spam" from now on. When you then later say:

x = None

this does *not* convert spam into None, what it does is cuts the binding 
between name "x" and spam, and binds the name "x" to None. The object 
spam still exists.

If there are no other references to spam, then the garbage collector 
automatically destroys the spam object. But if there are still references 
to the spam object, then it will remain alive, and the gc will keep 
tracking it.

References can include:

* name bindings: x = spam

* list items: mylist.append(spam)

* dict keys and values: mydict[spam] = 42; mydict["key"] = spam

* attributes: myobject.attr = spam

* default values to function parameters: def foo(a, b=spam): ...

and probably others.


> What I'm try to do is to remove this object  from the list. but I do
> know how..


If you have a com object being tracked by the garbage collector, that 
means that it is still in use, somewhere in your program. You can't just 
tell the gc to stop tracking it. You need to find where you still have a 
reference to the object.


> the .__del__() method dose not work on this object...

The __del__ method does not delete an object. Remember, objects are only 
deleted when there are no references to it. Otherwise you could have some 
code that tries to use a deleted object, and you would get a system crash 
or BSOD. The __del__ method is called only when the object is *just 
about* to be deleted.

In general, you should avoid using __del__ methods. They interfere with 
the garbage collector's ability to resolve cycles. The problem is, what 
happens if an object contains a link to itself?

x = []
x.append(x)

Such cycles could be very deep:

a = []; b = []; c = []; d = []; ... y = []; z = []
a.append(b)
b.append(c)
c.append(d)
...
y.append(z)
z.append(a) ### a cycle appears ###

Now you have a cycle of 26 objects, each of which indirectly refers to 
itself.

Normally, Python's garbage collector can recognise such a cycle and 
safely break it, which then allows all of the objects to be deleted. But 
if you add a __del__ method to *even one* object, Python can no longer 
safely break the cycle, and so the objects will remain alive even when 
you can no longer reach them from your code.

So, in general, avoid the __del__ method unless you absolutely need it.


-- 
Steven



More information about the Python-list mailing list