473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.NET CLR 1.1 does not release the COM object reference.

We have a project in .NET 1.1 , some of the .NET methods take COM
interface reference pointer as a parameter, somehow we have to call
Marshal.ReleaseComObject to release the COM Object, otherwise the COM
object will never get release, Even we call gc.Collect()

But the same code compiles in .NET 2.0 works without the
Marshal.ReleaseComObject. (Unfortunately the project is supposed to
run on .NET 1.1.)
Is this a known problem? What is the remedy besides
Marshal.ReleaseComObject? (We know it is dangerous to call
Marshal.ReleaseComObject since we do not have total control of the
COM
interface pointer )
Thanks in advance.
John

May 16 '07 #1
4 1729
John,

Regardless of version, you should be calling ReleaseComObject on
references to COM objects that you have when you are done with them. COM
depends on reference counting, and while garbage collection will ultimately
take care of stray references that you have (and properly decrement the
reference count, which will ultimately handle the disposing of the COM
object that the Runtime Callable Wrapper holds on to), it isn't a good idea
to not release the objects when you are done with them.

In other words, don't remove the call in .NET 2.0 because you think it
works.

As for .NET 1.1, the ReleaseComObject method existed on the Marshal
class in that version of the framework, so there is no reason you can't use
it there.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<jo*****@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
We have a project in .NET 1.1 , some of the .NET methods take COM
interface reference pointer as a parameter, somehow we have to call
Marshal.ReleaseComObject to release the COM Object, otherwise the COM
object will never get release, Even we call gc.Collect()

But the same code compiles in .NET 2.0 works without the
Marshal.ReleaseComObject. (Unfortunately the project is supposed to
run on .NET 1.1.)
Is this a known problem? What is the remedy besides
Marshal.ReleaseComObject? (We know it is dangerous to call
Marshal.ReleaseComObject since we do not have total control of the
COM
interface pointer )
Thanks in advance.
John
May 16 '07 #2
Nicholas,Thanks so much for the reply, two more questions.
1) How come in .NET 1.1, the COM object is not released even after I
called gc.Collect multiple times as following

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

If the above code releases the COM object, I would not be so puzzled.

But in .NET 2.0 it was release right away.

2) In our code, we have one method, take the COM object, stored it in
an ArrayList, in the second method , we take the same COM object, here
we search the ArrayList, remove the COM object from the ArrayList, it
is here where I call the ReleaseComObject

For the same .Net method. One is called by VC++ client, I only need to
call ReleaseComObject once, but for the VB client I have to call
ReleaseComObject 3 times before the object is release

If I keep calling ReleaseComObject until the return value is 0, then
I will get an exception
"COM object that has been separated from its underlying RCW can not
be used."

That got me really worried , why 3 times ? Is it possible for me to
get the exception by calling ReleaseComObject 3 times? (Because we
have not control how the client is using our framework)

Please advice.
Thanks.

On May 15, 7:03 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
John,

Regardless of version, you should be calling ReleaseComObject on
references to COM objects that you have when you are done with them. COM
depends on reference counting, and while garbage collection will ultimately
take care of stray references that you have (and properly decrement the
reference count, which will ultimately handle the disposing of the COM
object that the Runtime Callable Wrapper holds on to), it isn't a good idea
to not release the objects when you are done with them.

In other words, don't remove the call in .NET 2.0 because you think it
works.

As for .NET 1.1, the ReleaseComObject method existed on the Marshal
class in that version of the framework, so there is no reason you can't use
it there.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<john...@gmail.comwrote in message

news:11*********************@n59g2000hsh.googlegro ups.com...
We have a project in .NET 1.1 , some of the .NET methods take COM
interface reference pointer as a parameter, somehow we have to call
Marshal.ReleaseComObject to release the COM Object, otherwise the COM
object will never get release, Even we call gc.Collect()
But the same code compiles in .NET 2.0 works without the
Marshal.ReleaseComObject. (Unfortunately the project is supposed to
run on .NET 1.1.)
Is this a known problem? What is the remedy besides
Marshal.ReleaseComObject? (We know it is dangerous to call
Marshal.ReleaseComObject since we do not have total control of the
COM
interface pointer )
Thanks in advance.
John- Hide quoted text -

- Show quoted text -

May 16 '07 #3
Nicholas ,
Thanks so much for the reply, two more following questions.
1) How come in .NET 1.1, the COM object is not released even after I
called gc.Collect multiple times as following

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

If the above code releases the COM object, I would not be so puzzled.

But in .NET 2.0 it was release right away.

2) In our code, we have one method, take the COM object, stored it in
an ArrayList, in the second method , we take the same COM object, here
we search the ArrayList, remove the COM object from the ArrayList, it
is here where I call the ReleaseComObject

For the same .Net method. One is called by VC++ client, I only need
to call ReleaseComObject once, but for the VB client I have to call
ReleaseComObject 3 times before the object is release

If I keep calling ReleaseComObject until the return value is 0, then
I will get an exception
"COM object that has been separated from its underlying RCW can not
be used."

That got me really worried , why 3 times ? Is it possible for me to
get the exception by calling ReleaseComObject 3 times? (Because we
have not control how the client is using our framework)

Please advice.
Thanks so much for your help.
John

On May 15, 7:03 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
John,

Regardless of version, you should be calling ReleaseComObject on
references to COM objects that you have when you are done with them. COM
depends on reference counting, and while garbage collection will ultimately
take care of stray references that you have (and properly decrement the
reference count, which will ultimately handle the disposing of the COM
object that the Runtime Callable Wrapper holds on to), it isn't a good idea
to not release the objects when you are done with them.

In other words, don't remove the call in .NET 2.0 because you think it
works.

As for .NET 1.1, the ReleaseComObject method existed on the Marshal
class in that version of the framework, so there is no reason you can't use
it there.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<john...@gmail.comwrote in message

news:11*********************@n59g2000hsh.googlegro ups.com...
We have a project in .NET 1.1 , some of the .NET methods take COM
interface reference pointer as a parameter, somehow we have to call
Marshal.ReleaseComObject to release the COM Object, otherwise the COM
object will never get release, Even we call gc.Collect()
But the same code compiles in .NET 2.0 works without the
Marshal.ReleaseComObject. (Unfortunately the project is supposed to
run on .NET 1.1.)
Is this a known problem? What is the remedy besides
Marshal.ReleaseComObject? (We know it is dangerous to call
Marshal.ReleaseComObject since we do not have total control of the
COM
interface pointer )
Thanks in advance.
John- Hide quoted text -

- Show quoted text -

May 16 '07 #4
See inline:
1) How come in .NET 1.1, the COM object is not released even after I
called gc.Collect multiple times as following

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

If the above code releases the COM object, I would not be so puzzled.

But in .NET 2.0 it was release right away.
Without seeing the rest of the code, it is impossible to say, but the
only reason the COM reference would be sticking around is if something was
holding a reference to the wrapper and that wrapper had not been passed to
ReleaseComObject.
2) In our code, we have one method, take the COM object, stored it in
an ArrayList, in the second method , we take the same COM object, here
we search the ArrayList, remove the COM object from the ArrayList, it
is here where I call the ReleaseComObject

For the same .Net method. One is called by VC++ client, I only need
to call ReleaseComObject once, but for the VB client I have to call
ReleaseComObject 3 times before the object is release

If I keep calling ReleaseComObject until the return value is 0, then
I will get an exception
"COM object that has been separated from its underlying RCW can not
be used."

That got me really worried , why 3 times ? Is it possible for me to
get the exception by calling ReleaseComObject 3 times? (Because we
have not control how the client is using our framework)
I don't think you should be calling ReleaseComObject three times. As a
matter of fact, you shouldn't be calling it at all in this case. If the
same object is going to be placed into the ArrayList, then you shouldn't be
calling ReleaseComObject on the object you take out, as it is going to
invalidate the wrapper that you are placing into the ArrayList in its place.
Basically, you should be releasing the object when you are done with it
(which you aren't in this case).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
Please advice.
Thanks so much for your help.
John

On May 15, 7:03 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>John,

Regardless of version, you should be calling ReleaseComObject on
references to COM objects that you have when you are done with them. COM
depends on reference counting, and while garbage collection will
ultimately
take care of stray references that you have (and properly decrement the
reference count, which will ultimately handle the disposing of the COM
object that the Runtime Callable Wrapper holds on to), it isn't a good
idea
to not release the objects when you are done with them.

In other words, don't remove the call in .NET 2.0 because you think
it
works.

As for .NET 1.1, the ReleaseComObject method existed on the Marshal
class in that version of the framework, so there is no reason you can't
use
it there.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<john...@gmail.comwrote in message

news:11*********************@n59g2000hsh.googlegr oups.com...
We have a project in .NET 1.1 , some of the .NET methods take COM
interface reference pointer as a parameter, somehow we have to call
Marshal.ReleaseComObject to release the COM Object, otherwise the COM
object will never get release, Even we call gc.Collect()
But the same code compiles in .NET 2.0 works without the
Marshal.ReleaseComObject. (Unfortunately the project is supposed to
run on .NET 1.1.)
Is this a known problem? What is the remedy besides
Marshal.ReleaseComObject? (We know it is dangerous to call
Marshal.ReleaseComObject since we do not have total control of the
COM
interface pointer )
Thanks in advance.
John- Hide quoted text -

- Show quoted text -


May 16 '07 #5

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

Similar topics

3
4420
by: WinstonSmith | last post by:
Hello everyone, I got a problem about GC when creating large fields (some MB), set reference to null and call GC.Collect. Not all virtual mem is released. Situation improved in .net 1.1 but not...
10
4266
by: Rich | last post by:
Hello, I have not been working with ASP for too long at this time and am not real familiar with a lot of things about ASP. I have searched for articles on the following question but not come up...
4
16566
by: Mullin Yu | last post by:
as subject. i instantiate a COM object at my c# application like below IDMObjects.Library oLib = new IDMObjects.LibraryClass(); should i need to close it or set it to nothing/null to release...
17
2660
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
9
8543
by: Frank Rizzo | last post by:
I understand the basic premise: when the object is out of scope or has been set to null (given that there are no funky finalizers), executing GC.Collect will clean up your resources. So I have...
51
3879
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
6
9117
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++...
2
1228
by: johnxhc | last post by:
We have a project in .NET 1.1 , some of the .NET methods take COM interface reference pointer as a parameter, somehow we have to call Marshal.ReleaseComObject to release the COM Object, otherwise...
10
8128
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
0
7089
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
7339
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
7463
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
5581
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,...
0
4678
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...
0
3168
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...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
0
389
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...

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.