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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:17:30+00:00 2026-06-05T20:17:30+00:00

I have a native object (C++) that has a gcroot pointer to a managed

  • 0

I have a native object (C++) that has a gcroot pointer to a managed object (C#).

class SomeNativeClass {
    gcroot<SomeManagedClass ^> managedClass;
};

Question
When I delete a native instance of this class in native code delete(someNativeClass) that I previously allocated, will the managedClass instance get garbage collected or should I explicitly delete it (in the native destructor) 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-05T20:17:31+00:00Added an answer on June 5, 2026 at 8:17 pm

    If only delete the native object and the managed is not referenced anywhere else, would it be garbage collected?

    That’s the essence of garbage collection. The collector destroys an object when it cannot find a reference to the object. It is done a bit differently when you use the gcroot<> template class, the “normal” way the collector finds references to managed objects is by discovering them in cpu registers and the stack of managed code and the managed heap. That can’t work in native code, the gcroot<> class helps out.

    The gcroot<> constructor calls GCHandle::Alloc() to allocate an explicit reference to the object. Its destructor calls GCHandle::Free() to remove it. Being able to allocate these “tracking handles” is a secondary mechanism in the CLR, it maintains a table for them that the collector consults in addition to references it finds itself.

    The net effect is that when your class destructor runs, it will automatically call the gcroot<> destructor. Object reference gone. If that was the only reference, highly likely in this case, the next garbage collection sweep destroys the object. Automatically. Later.

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

Sidebar

Related Questions

I have an object that uses some underlying native resources, and has a pointer
This is probably a multi-part question. Background: we have a native (c++) library that
I have a C# program that uses a native C++ COM object. Visual Studio
I have a Django site, with an Item object that has a boolean property
I have a native Visual C++ COM object and I need to make it
I have a native/unmanaged DLL and it has a CreateObject function which returns a
Some Windows native applications have a question mark icon on the title bar. It's
I have a MongoMapper model called LogInfo that has a number of fields already
I currently have a data model set up where each School object has a
I have a native function call that calls my c++ code and this code

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.