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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:40:41+00:00 2026-06-14T22:40:41+00:00

I have several unmanaged c++ classes that reference each other. In order to avoid

  • 0

I have several unmanaged c++ classes that reference each other. In order to avoid dangling pointers when an instance is released, I’m using boost smart pointers, mostly shared_ptr. So far so good.

However I also have a wrapper in C++/CLI, in which almost every unmanaged class has its managed equivalent to expose it to .NET applications. The wrapper is quite simple, however I was forced to use an unsafe pointer to reference UnmanagedClass* from ManagedClass. I cannot use boost::shared_ptr as a member of ManagedClass, because the CLR does not support unmanaged types as class members (only pointers to them).

Example: class Car which contains 4 instances of class Wheel. Each of the 5 unmanaged instances have 5 managed equivalent instances. Unmanaged Car might need to change its wheels, it deletes 4 instances of unmanaged Wheel and creates 4 new ones. Managed Car asks for the new unmanaged Wheel to create 4 new managed instances of Wheel.

However, the 4 old managed instances of Wheel are still in scope in the managed world, and now contain a dangling pointer to the old unmanaged instances. Any ideas how to realize that the native equivalent of a wrapper was disposed? An easy task with smart pointers. Can I use them in managed code?

  • 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-14T22:40:42+00:00Added an answer on June 14, 2026 at 10:40 pm

    Why not use internal instance counting? Create a base native class:

    class Base
    {
        int refCount = 0;
    
    public:
        void AddRef() { refCount++; }
        void Release() { refCount--; if (refCount == 0) delete this; }
    }
    

    Then use it as a base class for all of these and remember to call AddRef when you acquire an instance of class and Release() when you no longer need it (for example in destructor). You would have to do the same in you C++/CLI classes; implement a IDisposable interface and clean the held native class instances in the Dispose() method. It shouldn’t be much problematic to use, because you only would have to call AddRef once inside constructors and Release once inside destructors/Dispose.

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

Sidebar

Related Questions

I have been provided with an unmanaged C++ DLL that contains several classes. I
I have several classes that I serialize/deserialize, each with a number of properties, some
I have several Select which depend on each other if I select a value
I have several foreach loops after each other in a larger foreach loop.. What
I have several projects, some using managed code and some using only unmanaged. All
I have several loops that I wrote using the traditional syntax foreach(x in xs)
I have a native/unmanaged C++ library with a number of classes that I would
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several different numbers in a group that range in sizes and would

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.