Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:26:11+00:00 2026-06-07T18:26:11+00:00

I have a C# client that consumes interfaces from a native C++ COM server

  • 0

I have a C# client that consumes interfaces from a native C++ COM server dll. The DLL implements 4 interfaces. These 4 interfaces are implemented by 4 coclasses in the DLL. But only 1 coclass is exposed to the client. Interfaces 2,3,4 are returned to the client by one of the methods in interfaces 1.

C++ COM server:

interface IFace1: IUnknown{
HRESULT CreateOtherInterface([in] REFIID iidFace, [out, iid_is(iidFace)] void** ppOut);
};

coclass ClassIFace1
{
    [default] interface IFace1;
};

C# Client:

ClassIFace1 Face1Obj = new ClassIFace1();

IFace1 Face1Ctrl = (IFace1)Face1Obj; 

IFace2 Face2Ctrl = null;
IntPtr Face2IntPtr = new IntPtr();

Face1Ctrl.CreateOtherInterface(Face2Guid, out Face2IntPtr);
Face2Ctrl = (IFace2)Mashal.PtrToStructure(Face2IntPtr);

//Consume Face2Ctrl

if(Face1Obj != null)
{
    Marshal.ReleaseComObject(Face1Obj);
}

As IFace2, IFace3 and IFace4 does not share the same coclass as IFace1, I suspect the Marshal.ReleaseComObject(Face1Obj) line will only destruct ClassIFace1 object but not ClassIFace2, ClassIFace3, ClassIFace4 objects and results in memory leaks. Is there any way to solve this? Or the Marshal.ReleaseComObject(Face1Obj) actually destroy other COM objects as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:26:12+00:00Added an answer on June 7, 2026 at 6:26 pm

    Like what Hans said, CreateOtherInterface looks very odd. Normally, you don’t need to create it by yourself. All you need to do is to make sure client can access all four coclasses. Then, the Activator.CreateInstance or the native CoCreateInstance will do the right thing for you. Another choice is to expose one single coclass and have that single coclass supporting all four interfaces.

    However, since you mentioned that only 1 coclass is exposed to client, I am imagining there are some strange reasons that the TLB file consumed by the client doesn’t see the other 3 coclasses or the other 3 coclasses were not registered properly but discovered by the first coclass in some proprietary ways. I am also assuming that you cannot modify server side implementation.

    Given all these assumption, here is my answer. The reference counts are maintained within the 4 coclasses independently. So, releasing the reference on the first coclass won’t decrement the reference counts in the other three coclasses.

    There are some more things you need to pay attention. You are using Marshal.ReleaseComObject(Face1Obj) to release the first coclass. You can do that because the first coclass was wrapped by a Runtime Callable Wrapper (RCW). Like what Martin said, even if you don’t call the Marshal.ReleaseComObject(), .NET runtime will do it for you when garbabge collection occurs.

    However, Face2Ctrl is obtained differently. It’s not wrapped by a RCW. You are treating the returned pointer as a structure directly. This doesn’t sound right to me because you may have issues on memory alignment and data marshalling. What you want to do might be calling Marshal.GetObjectForIUnknown which will return a RCW for you. Once you get your RCW, you can call Marshal.ReleaseComObject() to release your RCW in timely manner.

    If the implementation of CreateOtherInterface is like QueryInterface, which always AddRef on the returned interface, you should call Marshal.Release on the returned interface once you are done with Face2Obj. Marshal.ReleaseComObject() is not enough because it just releasees the reference count added by the RCW but in this case, you need more one call on IUnknown.Release

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a WCF inspector in my client application that consumes numerous web
I have a client program that consumes a web service. It works quite well
I have this jsp client that consumes a webservice. The problem with the client
I have a windows forms client that consumes an ASP.net web service. It's my
I have a .NET Web Service running in VS2005 and a client that consumes
I have a WCF service. now, I need to create a client that consumes
I have to write a client (in C++) that consumes REST apis of a
I have a .Net web service client that consumes a Java web service. When
I am writing a JMS client that consumes from a Queue. My broker is
If I have an application that that produces data slowly but consumes it quickly,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.