Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

Marshal.GetTypedObjectForIUnknown and reference counts

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 23 Mar 2007 13:30 GMT
Hello,

Say I have an interface from IUnknown::QueryInterface that I want
available in my .Net program. When QueryInterface returns, I am
responsible for Release()ing it, but depending on whether
GetTypedObjcetForIUnknown has already wrapped that object, it may or may
not increase the reference count on the COM object. How do I know when
to Release()?
Michael Phillips, Jr. - 23 Mar 2007 15:52 GMT
Per the MSDN documentation, the object returned is a runtime callable
wrapper.

The runtime callable wrapper maintains and releases the interface pointers.
See:
http://msdn2.microsoft.com/en-us/library/8bwh56xe.aspx

> Hello,
>
[quoted text clipped - 4 lines]
> not increase the reference count on the COM object. How do I know when to
> Release()?
Steve - 26 Mar 2007 15:11 GMT
Yes, I realise that. But consider this:

- IUnknown::QueryInterface returns an IntPtr to an interface. The
reference count is 1.
- GetTypedObjectForIUnknown is called to generate a RCW. It generates a
new RCW, now the reference count is 2.
- I use Marshal.ReleaseComObject to release the original reference.

That's all fine. We have 1 reference to the interface, that of the RCW.
Now consider this situation.

- IUnknown::QueryInterface returns an IntPtr to an interface. The
reference count is N.
- GetTypedObjectForIUnknown is called to generate a RCW. It has already
generated a RCW for this object in the past so the reference count is
unchanged.
- I use Marshal.ReleaseComObject to release the original reference. This
decreases the count back to N-1, prematurely freeing the object.

Am I misunderstanding something fundamental here?

Regards
Steve

> Per the MSDN documentation, the object returned is a runtime callable
> wrapper.
[quoted text clipped - 11 lines]
>> not increase the reference count on the COM object. How do I know when to
>> Release()?
Michael Phillips, Jr. - 26 Mar 2007 16:09 GMT
COM rules always apply.

For unmanaged COM objects, you are responsible for the reference count
bookeeping per the COM guidelines.

For managed COM objects that use the RCW mechanism, the RCW handles the
reference count bookeeping per the COM guidelines.

> Yes, I realise that. But consider this:
>
[quoted text clipped - 35 lines]
>>> not increase the reference count on the COM object. How do I know when
>>> to Release()?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.