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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:56:20+00:00 2026-06-03T21:56:20+00:00

In some C++/CLI code I have a native class which has a factory method

  • 0

In some C++/CLI code I have a native class which has a factory method GetWrapper() for creating its own managed .NET wrapper object. Internally, it holds a weak reference to its wrapper via GCHandle. When GetWrapper() is called, the GCHandle is checked and either a handle to the existing wrapper is returned, or (if it does not point to an object anymore, because the old wrapper object has been destroyed by the Garbage Collector) a new one is created an returned.

// .h
class NativeClass
{
public:
    WrapperClass^ GetWrapper();
private:
    WrapperClass^ GetNewWrapper();
    GCHandle m_wrapperGCHandle;
};

// .cpp
WrapperClass^ NativeClass::GetWrapper()
{
    if(m_wrapperGCHandle.IsAllocated)
    {
        try
        {
            WrapperClass^ wrapper = nullptr;
            wrapper = dynamic_cast<WrapperClass^>(wrapperGCHandle.Target);

            if(wrapper == nullptr)
            {
                return GetNewWrapper();
            }
            else
            {
                return wrapper;
            }
         }
         catch(System::InvalidOperationException^)
         {
             return GetNewWrapper();
         }
    else
    {
        return GetNewWrapper();
    }
}

WrapperClass^ NativeClass::GetNewWrapper()
{
    WrapperClass^ wrapper = gcnew WrapperClass(/*some args*/);
    m_wrapperGCHandle = GCHandle::Alloc(wrapper, GCHandleType::Weak);
}

The strange thing now is that m_wrapperGCHandle.IsAllocated always returns true, even if the wrapper has been garbage-collected. The MSDN tells to “Use this property when using Weak handles to determine if the GCHandle is still available.”. But it’s always true. If it’s not available then the Target is a nullptr instead.

Am I missing something or is the MSDN wrong?

  • 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-03T21:56:21+00:00Added an answer on June 3, 2026 at 9:56 pm

    My read of the MSDN doco is that m_wrapprGCHandle.IsAllocated will return true until m_wrapperGCHandle.Free is called – the IsAllocated property is checking for the state of the handle rather than the state of the reference held by the handle.

    As you have noted, m_wrapperGCHandle.Target is null when the object has been garbage collected. I have used a similar method to what you have in your example code to generate managed wrapper classes, and I always check to see if Target is null and regenerate the wrapper object if Target is null.

    Also, a suggestion … it looks to me as if you have a handle leak in your code because you are calling GCHandle::Alloc without a corresponding m_wrapperGCHandle.Free call. Try putting the call to Alloc in the class constructor and the call to Free in the destructor:

    NativeClass::NativeClass()
    {
        m_wrapperGCHandle = GCHandle::Alloc(nullptr, GCHandleType::Weak);
    }
    
    NativeClass::~NativeClass()
    {
        m_wrapperGCHandle.Free();
    }
    

    then your GetNewWrapper method is simply:

    WrapperClass^ NativeClass::GetNewWrapper()
    {
        m_wrapperGCHandle.Target = gcnew WrapperClass(/*some args*/);
    }
    

    and you can remove the if(m_wrapperGCHandle.IsAllocated) - else chain from the GetWrapper method.

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

Sidebar

Related Questions

I have an assembly, written in C++\CLI, which uses some of enumerations, provided by
I have this API - which I managed to carryout some API steps solely
I have a class, A , in C++/CLI which derives from a templated base
I have a native C DLL being invoked by a C++/CLI object which is
I have a project which uses C++/CLI to implement a GUI and some background
I have a custom plugin for 3ds Max that interfaces with some managed code
I've got some C++/CLI software which is all nice and documented in a C#'ish
I have C++/CLI code and I'm using Visual Studio 2008 Team Suite Code Coverage.
I have a situation where I've wrapped a Native C++ DLL with C++/CLI for
I have to include a (managed) C++/CLI component in one of my ASP.NET projects,

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.