Handle of COM object.

P

Piotrekk

Hi

I am using INCA RTX library which i've added to my project resources.
Then i created an object i need to release it manually. I have found
example in MSND - how to release com object without using Garbace
collector ( because it's simply to slow ). Constuctor of MyResource
which inherits from IDisposable gets one argument - Pointer to an
external unmanaged resource. How to get this Pointer from created com
object?

Thanks
PK
 
N

Nicholas Paldino [.NET/C# MVP]

Piortekk,

Why do you need to get a pointer for the COM object? Why not just pass
the COM object to the static ReleaseComObject method on the Marshal class?
 
P

Piotrekk

Why do you need to get a pointer for the COM object? Why not just pass
the COM object to the static ReleaseComObject method on the Marshal class?

INCA RTX objects are very memory consuming and GC is simply to slow.
I have read this article:
http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx

MyResource constuctor gets pointer to unmanaged object as a parameter.
Thus i thought i will be able to destroy RTX object manually by
calling MyResource object's Dispose Method.

What do you think?
 
P

Piotrekk

I am not sure if ReleaseComObject will release com object
independently of GarbageCollector.
 
N

Nicholas Paldino [.NET/C# MVP]

If you have a wrapper to a COM object, then calling ReleaseComObject
will decrement the reference count on the com object, and the object should
delete itself when the reference count reaches zero.

If you have a pointer to a COM object (in the form of an IntPtr), then
you can pass that to the Release method on the Marshal class and it will do
the same thing.
 
P

Piotrekk

But there is one problem with ReleaseComObject. It will not release
the memory in a second. When I am finished with COM object and pass it
to ReleaseComObject I am done with the pointer. But the memory will
not be released immediately.


Or maybe I am wrong and memory will be released in a moment?

I am in need of releasing memory right after I stop using COM. When
using few 500 mb resources I really shouldn't wait for GC to collect
it...
As it is written in link i gave you ( MyResource inheriting from
IDisposable ) calling Dispose method should release memory by calling
API function private extern static Boolean CloseHandle(IntPtr handle);
- i haven't tried that yet.

Right now I am a bit confused if ReleaseComObject will release the
memory immidiately.
 
N

Nicholas Paldino [.NET/C# MVP]

Piotrekk,

You should only release the unmanaged handles, or decrement the
reference count on the COM object that you are holding when you are done
with it. The wrapper for a COM object will still be in memory (which should
be light), but the COM object itself should be disposed of at that point.

You shouldn't have to call Collect at all, rather, you should be letting
the CLR collect garbage for you.
 

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