By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

COM Interop problem ( Mem leak )

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.