473,404 Members | 2,178 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,404 software developers and data experts.

Interop - Object Lifetime

Tim
I have a class called Manager. In the 'Initialize' method of the Manager
class, I create an instance of a COM class (say oComObject). What is the
lifetime of oComObject. Will it be available as long as the 'Manager' object
is in memory?

Is there any difference in object lifetime with and without Interop?
Dec 14 '05 #1
2 1512
Tim,

In the Initialize method, do you store the reference in a field in the
class, or is it a variable on the stack? If it is on the stack, then when
the reference is let go, it makes the wrapper eligible for GC, and the COM
object will be released on the next GC. If it is stored in the object, then
when your object is eligible for GC, then the wrapper is eligible for GC.

Both cases assume that you do not pass the reference outside of the
method or class.

If you are using your COM object in your method only, then you can pass
the object to the static ReleaseComObject method on the Marshal class, and
it will release the object (assuming that you are the only one who had a
reference to it).

If you are storing it in your class, then you should call the
ReleaseComObject method in an implementation of IDisposable.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
I have a class called Manager. In the 'Initialize' method of the Manager
class, I create an instance of a COM class (say oComObject). What is the
lifetime of oComObject. Will it be available as long as the 'Manager'
object
is in memory?

Is there any difference in object lifetime with and without Interop?

Dec 14 '05 #2
Tim
Hi Nicholas,

Thank you for the informative response. In my case, the ComObject's
reference is stored in a field in the class. Then based on your respose, I
need to call ReleaseComObject method in an implementation of IDisposable.

But, Why do I have to call ReleaseComObject? Will it not be released
automatically when it is eligible for GC? Also, can I call ReleaseComObject
in the Terminate method of the class or is it essential to do so in an
implementation of IDisposable?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tim,

In the Initialize method, do you store the reference in a field in the
class, or is it a variable on the stack? If it is on the stack, then when
the reference is let go, it makes the wrapper eligible for GC, and the COM
object will be released on the next GC. If it is stored in the object, then
when your object is eligible for GC, then the wrapper is eligible for GC.

Both cases assume that you do not pass the reference outside of the
method or class.

If you are using your COM object in your method only, then you can pass
the object to the static ReleaseComObject method on the Marshal class, and
it will release the object (assuming that you are the only one who had a
reference to it).

If you are storing it in your class, then you should call the
ReleaseComObject method in an implementation of IDisposable.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
I have a class called Manager. In the 'Initialize' method of the Manager
class, I create an instance of a COM class (say oComObject). What is the
lifetime of oComObject. Will it be available as long as the 'Manager'
object
is in memory?

Is there any difference in object lifetime with and without Interop?


Dec 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: johny smith | last post by:
Suppose there is a policy that all objects are statically declared. For example: static Car car(); Then, is there a reason to have a destructor defined for the class Car. It would seem...
8
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
1
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer,...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
1
by: Brendan Grant | last post by:
I’ve got a C# library that I’ve built into a COM component that will be used from a VC++ 6 application and while the creation of the COM object side of things seem to be working fine, using the...
3
by: nagashre | last post by:
class A { public: A():a(0), b(0){} handleMyMsg( char* aa, char*bb); private: processMessage();
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
6
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.