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 5934797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:09:52+00:00 2026-05-22T15:09:52+00:00

I have an unmanged class and a ref class which ar logically connected: public

  • 0

I have an unmanged class and a ref class which ar logically connected:

public ref class RefBlah
{
   ~RefBlah();
   !RefBlah();

   internal:

   UnManagedBlah* m_unmanaged;
}

public class UnManagedBlah
{
    public:
    gcroot<RefBlah^> refBlah;
}

The RefBlah class always creates an instance of UnManagedBlah which holds a reference to the object that created it.

Now, when I create an instance of RefBlah in a C# application, it just doesn’t get freed when it get out of scope. (I’ve waited and seen all the other objects get freed, but it refuses to remove itself).

As far as I know, if they were both regular .Net objects, they would both be collected when the class gets out of scope because although the reference count does not reach 0. And that’s because there’s no refernce root to the objects from the main stack.

Does .NET GC treat references from unmanaged classes differently ?

How can I change the design so that RefBlah will be 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-05-22T15:09:52+00:00Added an answer on May 22, 2026 at 3:09 pm

    I think you have a circular reference problem. RefBlah won’t get GC’d until the reference in UnManagedBlah is GC’d which will only happen when you delete the pointer in RefBlah etc..

    If you need to have a reference inside the unmanaged class then perhaps it should be a weak reference? Have a look at the GCHandle struct:

    The GCHandle class is used with the
    GCHandleType enumeration to create a
    handle corresponding to any managed
    object. This handle can be one of four
    types: Weak, WeakTrackResurrection,
    Normal, or Pinned. When the handle has
    been allocated, you can use it to
    prevent the managed object from being
    collected by the garbage collector
    when an unmanaged client holds the
    only reference. Without such a handle,
    the object can be collected by the
    garbage collector before completing
    its work on behalf of the unmanaged
    client.

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

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a DLL, which is designed in C++, included in a C# project
I have an unmanaged class that I'm trying to dllexport from a managed DLL
In the following example, I get: error C2300: 'UnmanagedClass' : class does not have
I have a C# class library that contains methods that need to be used
I have the following unmanaged C++ code: MessageBox( NULL, strMessage, Cool Product, MB_RETRYCANCEL |
What are your thoughts about them? Sometimes I have to write unmanaged code at
I have a collection of unmanaged dlls with a C# wrapper around them that
Basically I have a bunch of unmanaged VC++ static libraries. And the VC++ GUI
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp

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.