473,788 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.createAC ertainObject()
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 1475
<bg***@yahoo.co mescribió en el mensaje
news:11******** **************@ v45g2000cwv.goo glegroups.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.createAC ertainObject()
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.getrefcou nt(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.co mescribió en el mensaje
news:11******** **************@ v45g2000cwv.goo glegroups.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.createAC ertainObject()
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.getrefcou nt(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.createAC ertainObject()
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
2466
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 find an explanation for is why str.join() doesn't automatically call str() on its arguments, so that e.g. str.join() would yield "1245", and ditto for e.g. user-defined classes that have a __str__() defined. All I've been able to find is a 1999...
23
5184
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
2983
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
24
7696
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 Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
8
1952
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 anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
11
2705
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 !! class AA {
13
9727
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
12
7215
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? class Trial { public: Trial() {
275
12406
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
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.