473,387 Members | 1,561 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.

COM Interop problem ( Mem leak )

Hi,

I have a .NET application that periodically creates COM objects, these
objects are created at one thread context and deleted in the other (
COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM objects, I
have used 'InteropServices.Marshal.ReleaseComObject' to force the COM Obj to
be deleted, The com wasn't deleted and the ref count was 97, all of the
references that i set for the object were removed, WHAT AM I DOING WRONG
here? why does none of the COM Objects i use get deleted? can it be a mem
leak in the .NET framework????

Any help would be appreciated...
--
Nadav
http://www.sophin.com
Nov 17 '05 #1
5 2161
Nadav,

You can't arbitrarily pass around COM objects between threads. They
require some very specific marshalling, depending on the the apartment type
that the COM object has.

Is the thread context that you create it in an STA thread (like say the
UI thread of your windows application), and then you pass it to your worker
thread (which is MTA by default)? If so, you simply can not do this. You
have to marshal those instances explictly across the thread boundary (or
rather, apartment boundary, which is really what it is).

How are you passing the com object between threads?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi,

I have a .NET application that periodically creates COM objects, these
objects are created at one thread context and deleted in the other (
COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM objects, I
have used 'InteropServices.Marshal.ReleaseComObject' to force the COM Obj
to
be deleted, The com wasn't deleted and the ref count was 97, all of the
references that i set for the object were removed, WHAT AM I DOING WRONG
here? why does none of the COM Objects i use get deleted? can it be a mem
leak in the .NET framework????

Any help would be appreciated...
--
Nadav
http://www.sophin.com

Nov 17 '05 #2
Well, the problem was resolved.
calling while(0 < InteropServices.Marshal.ReleaseComObject(MyComObj) );
caused the COM Obj to get released, Releasing a COM the way I have just
described is adviced in the MSDN documentation of
'InteropServices.Marshal.ReleaseComObject' BUT why should I do that? doesn't
the .NET framework know to trace ref-count by it-self? why setting ComObj =
null is not enough? it seems to me that this is some kind of a BUG, and
calling 'InteropServices.Marshal.ReleaseComObject' in a loop is just a
work-around....

Any comment would be appreciated.
--
Nadav
http://www.sophin.com
"Nadav" wrote:
Hi Nicholas,

Thanks for your immediate responce, Now all access to the COM Object is done
from a single thread ( the GUI thread ), still, I get the same results, ref
count = 98, why is that? whats may cause this problem ???
--
Nadav
http://www.sophin.com
"Nicholas Paldino [.NET/C# MVP]" wrote:
Nadav,

You can't arbitrarily pass around COM objects between threads. They
require some very specific marshalling, depending on the the apartment type
that the COM object has.

Is the thread context that you create it in an STA thread (like say the
UI thread of your windows application), and then you pass it to your worker
thread (which is MTA by default)? If so, you simply can not do this. You
have to marshal those instances explictly across the thread boundary (or
rather, apartment boundary, which is really what it is).

How are you passing the com object between threads?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi,

I have a .NET application that periodically creates COM objects, these
objects are created at one thread context and deleted in the other (
COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM objects, I
have used 'InteropServices.Marshal.ReleaseComObject' to force the COM Obj
to
be deleted, The com wasn't deleted and the ref count was 97, all of the
references that i set for the object were removed, WHAT AM I DOING WRONG
here? why does none of the COM Objects i use get deleted? can it be a mem
leak in the .NET framework????

Any help would be appreciated...
--
Nadav
http://www.sophin.com


Nov 17 '05 #3
Hi Nicholas,

Thanks for your immediate responce, Now all access to the COM Object is done
from a single thread ( the GUI thread ), still, I get the same results, ref
count = 98, why is that? whats may cause this problem ???
--
Nadav
http://www.sophin.com
"Nicholas Paldino [.NET/C# MVP]" wrote:
Nadav,

You can't arbitrarily pass around COM objects between threads. They
require some very specific marshalling, depending on the the apartment type
that the COM object has.

Is the thread context that you create it in an STA thread (like say the
UI thread of your windows application), and then you pass it to your worker
thread (which is MTA by default)? If so, you simply can not do this. You
have to marshal those instances explictly across the thread boundary (or
rather, apartment boundary, which is really what it is).

How are you passing the com object between threads?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
Hi,

I have a .NET application that periodically creates COM objects, these
objects are created at one thread context and deleted in the other (
COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM objects, I
have used 'InteropServices.Marshal.ReleaseComObject' to force the COM Obj
to
be deleted, The com wasn't deleted and the ref count was 97, all of the
references that i set for the object were removed, WHAT AM I DOING WRONG
here? why does none of the COM Objects i use get deleted? can it be a mem
leak in the .NET framework????

Any help would be appreciated...
--
Nadav
http://www.sophin.com


Nov 17 '05 #4
Nadav,

You shouldn't have to call ReleaseComObject in a loop like that.
However, I don't think it is a bug. Without seeing how you acquire, pass,
and release your COM object, it is impossible to tell.

Can you post a working example?

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

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
Well, the problem was resolved.
calling while(0 < InteropServices.Marshal.ReleaseComObject(MyComObj) );
caused the COM Obj to get released, Releasing a COM the way I have just
described is adviced in the MSDN documentation of
'InteropServices.Marshal.ReleaseComObject' BUT why should I do that?
doesn't
the .NET framework know to trace ref-count by it-self? why setting ComObj
=
null is not enough? it seems to me that this is some kind of a BUG, and
calling 'InteropServices.Marshal.ReleaseComObject' in a loop is just a
work-around....

Any comment would be appreciated.
--
Nadav
http://www.sophin.com
"Nadav" wrote:
Hi Nicholas,

Thanks for your immediate responce, Now all access to the COM Object is
done
from a single thread ( the GUI thread ), still, I get the same results,
ref
count = 98, why is that? whats may cause this problem ???
--
Nadav
http://www.sophin.com
"Nicholas Paldino [.NET/C# MVP]" wrote:
> Nadav,
>
> You can't arbitrarily pass around COM objects between threads.
> They
> require some very specific marshalling, depending on the the apartment
> type
> that the COM object has.
>
> Is the thread context that you create it in an STA thread (like say
> the
> UI thread of your windows application), and then you pass it to your
> worker
> thread (which is MTA by default)? If so, you simply can not do this.
> You
> have to marshal those instances explictly across the thread boundary
> (or
> rather, apartment boundary, which is really what it is).
>
> How are you passing the com object between threads?
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "Nadav" <Na***@discussions.microsoft.com> wrote in message
> news:49**********************************@microsof t.com...
> > Hi,
> >
> > I have a .NET application that periodically creates COM objects,
> > these
> > objects are created at one thread context and deleted in the other (
> > COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM
> > objects, I
> > have used 'InteropServices.Marshal.ReleaseComObject' to force the COM
> > Obj
> > to
> > be deleted, The com wasn't deleted and the ref count was 97, all of
> > the
> > references that i set for the object were removed, WHAT AM I DOING
> > WRONG
> > here? why does none of the COM Objects i use get deleted? can it be a
> > mem
> > leak in the .NET framework????
> >
> > Any help would be appreciated...
> > --
> > Nadav
> > http://www.sophin.com
>
>
>

Nov 17 '05 #5
Where do you get this ref count from?

Willy.

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
Hi Nicholas,

Thanks for your immediate responce, Now all access to the COM Object is
done
from a single thread ( the GUI thread ), still, I get the same results,
ref
count = 98, why is that? whats may cause this problem ???
--
Nadav
http://www.sophin.com
"Nicholas Paldino [.NET/C# MVP]" wrote:
Nadav,

You can't arbitrarily pass around COM objects between threads. They
require some very specific marshalling, depending on the the apartment
type
that the COM object has.

Is the thread context that you create it in an STA thread (like say
the
UI thread of your windows application), and then you pass it to your
worker
thread (which is MTA by default)? If so, you simply can not do this.
You
have to marshal those instances explictly across the thread boundary (or
rather, apartment boundary, which is really what it is).

How are you passing the com object between threads?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
> Hi,
>
> I have a .NET application that periodically creates COM objects, these
> objects are created at one thread context and deleted in the other (
> COmObjVar = null; ), FinalRelease NOT CALLED for any of the COM
> objects, I
> have used 'InteropServices.Marshal.ReleaseComObject' to force the COM
> Obj
> to
> be deleted, The com wasn't deleted and the ref count was 97, all of the
> references that i set for the object were removed, WHAT AM I DOING
> WRONG
> here? why does none of the COM Objects i use get deleted? can it be a
> mem
> leak in the .NET framework????
>
> Any help would be appreciated...
> --
> Nadav
> http://www.sophin.com


Nov 17 '05 #6

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

Similar topics

5
by: Trokey | last post by:
I am making interop calls to an object in a .NET component from a C++ program and am leaking memory... the following is some sample code: //////////////////////////////////////// // .NET...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
1
by: Kevin Frey | last post by:
We have an ActiveX control that we added as a reference to a *Console* C# testbed. The testbed functions, except that: a. On VS 2003, performance is fine, but on VS 2005, performance is reduced...
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
2
by: bbembi_de | last post by:
Hello, I use a C++ Dll in .Net 1.1 with Dllimport. I call one C++ method in a loop and the memory of my .Net application rises proportional. What am I doing wrong calling this method? ...
2
by: Derek Hart | last post by:
I am using late bound Microsoft Word integration with a vb.net winforms application. If I run code such as the following: Dim objWord As Object Dim objWrdDoc As Object Dim count As Integer...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.