COM Interop problem ( Mem leak )

G

Guest

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...
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
G

Guest

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 said:
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 said:
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]
- (e-mail address removed)

Nadav said:
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...
 
G

Guest

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 said:
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?
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Nadav said:
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 said:
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 said:
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]
- (e-mail address removed)

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...
 
W

Willy Denoyette [MVP]

Where do you get this ref count from?

Willy.

Nadav said:
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 said:
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]
- (e-mail address removed)

Nadav said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top