471,605 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 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 1437
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by johny smith | last post: by
1 post views Thread by Nadav | last post: by
16 posts views Thread by anonymous.user0 | last post: by
1 post views Thread by Brendan Grant | last post: by
3 posts views Thread by nagashre | last post: by
6 posts views Thread by better_cs_now | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.