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

  • SEARCH
  • Home
  • 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 8825235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:49:45+00:00 2026-06-14T06:49:45+00:00

Lately I was doing a little project that involved a DLL module (that was

  • 0

Lately I was doing a little project that involved a DLL module (that was created with C#) and that I needed to use in my application (that was written in unmanaged C++) For this to work I was using ATL/COM.

I noticed that even though I’m using a _com_ptr_t in my C++ application for handling my core COM interface, C# object’s destructor is called only when my application is closed.

Let me give you some source to make things a bit more clearer:

Some of my C# code:

[ComVisible(true)]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestCOM
{
    [DispId(1)]
    void Connect([In, MarshalAs(UnmanagedType.U2)] ushort value);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ITestCOMEvents))]
public partial class TestCOM : ITestCOM
{
   ...
   ~TestCOM()
   {
      MessageBox.Show("DESTRUCTOR");
   }
   ...
   public void Connect(ushort value)
   {
      ...
   }
}

I’m creating the .tlb file using something like this:
“RegAsm.exe TestCOM.dll /tlb:Test.tlb /codebase“

In my C++ header I have:

#import "C:\...\mscorlib.tlb"
#import "......\TestCOM.tlb" named_guids exclude("ISupportErrorInfo")

#include <afxdisp.h>
#include <atlcom.h>

class Unit : public ::IDispEventSimpleImpl<0, Unit, &__uuidof(TestCOM::ITestCOMEvents)>
{
public:
   BEGIN_SINK_MAP(Unit)
      SINK_ENTRY_INFO(0, __uuidof(TestCOM::ITestCOMEvents), 0x1, OnEventCallback, &OnEventCallbackDef)
   END_SINK_MAP()

   ...

private

   TestCOM::ITestCOMPtr mTestCOM;
   // NOTE: This would be the same as "_com_ptr_t<_com_IIID<TestCOM::ITestCOM,  &__uuidof(TestCOM::ITestCOM)> > mTestCOM;"
}

And my C++ source file I create my “mTestCOM” like this:

mTestCOM.CreateInstance(TestCOM::CLSID_TestCOM)

And basically that’s it.. I can use any of my C# “TestCOM” object’s functions like this:

mTestCOM->Connect(7);

The question is:
Why my C# TestCOM object’s destructor is called only when my application is closed, and not when my C++ “Unit” object is destroyed?

  • 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-14T06:49:45+00:00Added an answer on June 14, 2026 at 6:49 am

    Though I’m not familiar with C# and COM integration, I do know that destructors in C# work very differently to destructors in C++. A C# object is memory-managed and garbage-collected. This means that at some point after the object stops being referenced by the application it belongs to and becomes ‘unreachable’, the garbage collector will destroy it.

    So the first important thing is that the delay between an object being abandoned and the garbage collector destroying it is nondeterministic… it will happen “at some point in the future”, which may well be at the point the application terminates.

    Secondly, the garbage collector is not always running. There is the concept of “memory pressure”, when your application is allocating large chunks of memory and the free memory available to it is running out… at that point, the garbage collector will fire to get rid of old, unreachable objects. If your application does not allocate lots of memory it will not suffer from any memory pressure and the garbage collector will not need to run.

    If you want to deterministically clean up some of a managed object’s resources, you will need to use something like the IDisposable interface and call the Dispose method explicitly.

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

Sidebar

Related Questions

I've been doing some work lately on a project that makes extensive use of
I'm doing lately so much C++ that I forgot how to use Java. I'm
I'm doing things considered horrible by some lately, but I personally enjoy this kind
Lately, I've been doing a lot of interaction with unmanaged libraries and I keep
Doing some code reviews lately I came across a number of classes that have
I have been doing some Windows Assembly Language programming lately and I realized that
Sorry this is such a long question. Ive been doing lots of research lately
I've been doing it like this lately and find it so much better than
I've been doing development in TWIG lately. It is an html templating language that
Lately I have been doing a security pass on a PHP application and I've

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.