473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1458
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Leo Breebaart | last post by:
I've tried Googling for this, but practically all discussions on str.join() focus on the yuck-ugly-shouldn't-it-be-a-list-method? issue, which is not my problem/question at all. What I can't...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
11
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can anyone point out for me the reason? thanks !! ...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.