469,581 Members | 2,032 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,581 developers. It's quick & easy.

Why do I have to call del explicitly for com objects?

Hi,

I'm creating objects in my python script belonging to a COM object
which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll
is the concerned process. The problem is that the objects destructor
within the com object is not called if the object lives past a certain
number of seconds. For example, this function will not call the
destructor concerned with obj unless the sleep is commented out.

def fnction:
obj = comobj.createACertainObject()
obj.doStuff()
sleep(10)
obj.doMoreStuff()
#del obj

It seems to me that the GC forgets about obj after a certain amount of
time. I can force the destructor to be called using del obj at the end
of my function, but why do I have to call this explicitly?

Thanks for your help,

Barry.

Jan 19 '07 #1
2 1294
<bg***@yahoo.comescribió en el mensaje
news:11**********************@v45g2000cwv.googlegr oups.com...
I'm creating objects in my python script belonging to a COM object
which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll
is the concerned process. The problem is that the objects destructor
within the com object is not called if the object lives past a certain
number of seconds. For example, this function will not call the
destructor concerned with obj unless the sleep is commented out.

def fnction:
obj = comobj.createACertainObject()
obj.doStuff()
sleep(10)
obj.doMoreStuff()
#del obj
I don't understand the case.
del does not invoke a destructor, just decrements the object's reference
count. When the rc reaches zero, the object is a candidate for GC. That is,
"some time in the future", the GC would destroy it (unless it's part of a
circular reference chain...)
So, *inside* your function, there is a reference held by the local variable
obj. It is decremented automatically when you exit the function (and obj
gets out of scope) or if you explicitely use del.
You can use sys.getrefcount() to see how many references an object has. (The
output is +1 because getrefcount() has a temporary reference to the object
too).

pyx="Hello World"
pysys.getrefcount(x)
2

See how many references your obj have. After calling doStuff or doMoreStuff,
you can have more references if those functions store `self` somewhere, or
pass it to another method that does so.
It seems to me that the GC forgets about obj after a certain amount of
time. I can force the destructor to be called using del obj at the end
of my function, but why do I have to call this explicitly?
If del obj works at the end, exiting the function should work too. Both ways
you decrement the rc. There is something *more* in here.

--
Gabriel Genellina
Jan 19 '07 #2

Gabriel Genellina skrev:
<bg***@yahoo.comescribió en el mensaje
news:11**********************@v45g2000cwv.googlegr oups.com...
I'm creating objects in my python script belonging to a COM object
which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll
is the concerned process. The problem is that the objects destructor
within the com object is not called if the object lives past a certain
number of seconds. For example, this function will not call the
destructor concerned with obj unless the sleep is commented out.

def fnction:
obj = comobj.createACertainObject()
obj.doStuff()
sleep(10)
obj.doMoreStuff()
#del obj

I don't understand the case.
del does not invoke a destructor, just decrements the object's reference
count. When the rc reaches zero, the object is a candidate for GC. That is,
"some time in the future", the GC would destroy it (unless it's part of a
circular reference chain...)
So, *inside* your function, there is a reference held by the local variable
obj. It is decremented automatically when you exit the function (and obj
gets out of scope) or if you explicitely use del.
You can use sys.getrefcount() to see how many references an object has. (The
output is +1 because getrefcount() has a temporary reference to the object
too).

pyx="Hello World"
pysys.getrefcount(x)
2

See how many references your obj have. After calling doStuff or doMoreStuff,
you can have more references if those functions store `self` somewhere, or
pass it to another method that does so.
It seems to me that the GC forgets about obj after a certain amount of
time. I can force the destructor to be called using del obj at the end
of my function, but why do I have to call this explicitly?
If del obj works at the end, exiting the function should work too. Both ways
you decrement the rc. There is something *more* in here.

--
Gabriel Genellina
Thanks for the reply. I tried using a longer sleep before the del but
the destructor wasn't called this time. I guess del is not the issue
here what so ever. As far as I can see, the garbage collector forgets
about my object after a certain period of time. The fix i'm using now
is to use Destruct functions in my CoM object which I call explicitly.

def fnction:
obj = comobj.createACertainObject()
obj.doStuff()
sleep(10)
obj.doMoreStuff()
obj.Destruct()

I'd still love to know what the issue is here.

Thanks,

Barry.

Jan 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

46 posts views Thread by Leo Breebaart | last post: by
23 posts views Thread by Fabian Müller | last post: by
8 posts views Thread by trying_to_learn | last post: by
8 posts views Thread by vvenk | last post: by
11 posts views Thread by fourfires.d | last post: by
12 posts views Thread by Rahul | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.