Do I need to call delete on an IDisposable in C ++ / CLI

I got the impression that in the C ++ / CLI method, if the class I used implements IDisposable, dispose will automatically invoke magic when the object goes out of scope. I recently 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;
        }
    }
}

. -, delete . -, ++ delete , ++/CLI, , , nullptr delete, - same ++/CLI ( , , , ).

+3
1
  • .NET( , , , ), . delete , :

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

    msclr::auto_handle<> try..finally, :

    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
    
  • delete nullptr - , , ++ - if.

+5

All Articles