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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:38:43+00:00 2026-05-23T03:38:43+00:00

I had been under the impression that in a C++/CLI method if a class

  • 0

I had been under the impression that in a C++/CLI method if a class I was using implements IDisposable, the dispose is auto-magically called when the object goes out of scope. Recently I came across some code that looks like this:

void SomeClass::SomeMethod()
{
    DisposableObject^ myObject = nullptr;
    try
    {
         // Some code that creates a DisposableObject and assigns to myObject
    }
    finally
    {
        if (myObject != nullptr)
        {
            // this is instead of IDisposable.Dispose
            delete myObject;
        }
    }
}

My question is sort of two fold really. First, do I need to explicitly call delete on the object. Secondly, in pure C++ it is safe to call delete on a null object, does that behavior change in C++/CLI, just wondering because it seems like I don’t really need the nullptr check around the delete call if the behavior is the same in C++/CLI (I understand behavior the same is a relative term since the delete on a managed object is doing different things than on an unmanaged object).

  • 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-23T03:38:43+00:00Added an answer on May 23, 2026 at 3:38 am
    1. You never strictly need to dispose anything in .NET (unless the class is implemented incorrectly, e.g. lacks a finalizer when one is warranted), but you absolutely should whenever possible. Using stack semantics obviates the need to call delete directly when lazy initialization is not needed:

      void SomeClass::SomeMethod() {
          DisposableObject myObject;
          // ...
          // Some code that uses myObject
      } // myObject is disposed automatically upon going out of scope
      

      Using msclr::auto_handle<> in combination with stack semantics obviates the need for a try..finally when lazy initialization is needed:

      void SomeClass::SomeMethod() {
          msclr::auto_handle<DisposableObject> myObject;
          // ...
          // Some code that creates a DisposableObject and assigns to myObject
          // ...
          // Some code that uses myObject
      } // myObject is disposed automatically upon going out of scope
      
    2. Calling delete on a value of nullptr is completely safe, defined behavior, just as in C++ – no need for the if.

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

Sidebar

Related Questions

From previous experience I had been under the impression that it's perfectly legal (though
I had until recently been under the impression that the CDbl(x) operation in VB.NET
I had been programming under the assumption that, when calling a method in C#
I have been under the impression that processes on the operating system have three
I know, that the issue had been widely discussed before, but after 5 hours
I've been using sqlite3 for my database under development and my app has gotten
I think I'm missing something. I was under the impression that git's usage of
I've been living under a rock for some years, but finaly I had to
This has been fixed. I guess I had left :method => put in the
I've been having endless woes trying to deploy an NServiceBus application that I had

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.