Destructor not being called when leaving scope

后端 未结 8 473
离开以前
离开以前 2021-01-03 11:50

I am learning memory management in C++ and I don\'t get the why only some of the destructors are called when leaving scope. In the code below, only obj1 destructor is called

相关标签:
8条回答
  • 2021-01-03 12:29

    Destructors are called automatically when an object that was created on the stack goes out of scope.

    With dynamically allocated objects, you need to call delete obj. delete will automatically call your destructor for you.

    0 讨论(0)
  • 2021-01-03 12:30

    If you allocate a object using new

    obj2= new cl1;
    

    Then unless you call delete on it, its destructor won't be called implicitly.

    EDIT: As @David, meantions in comments, One may call destructor of an object explicitly but in my experience there is rarely a need to manually call the destructor unless one is using placement new version of new.

    Variables on stack are implicitly cleaned up(by calling their destructors) when their scope ends.

    Dynamically allocated objects are not implicitly cleaned, it is the responsibility of the user to clean them up explicitly calling delete.

    This is the very reason one should not use raw pointers but use smart pointers.

    0 讨论(0)
  • 2021-01-03 12:31

    Use std::unique_ptr or std::shared_ptr instead of raw pointer. It the best way to avoid memory leaks or double free.

    That is the right way in modern C++.

    int myfunc (cl1 *oarg) 
    {
        cout << "myfunc called" << std::endl;
        cl1 obj1(222,"NY");
        std::unique_ptr<cl1> obj2( new cl1 );
        oarg->disp();
    }
    
    0 讨论(0)
  • 2021-01-03 12:37

    Dynamically allocated objects are your responsibility - you need to explicitly clean them up. Automatic objects (such as obj1) are cleaned up when the scope exits, automatically. In this case, before the scope exits - explicitly call delete obj2. NOTE: this line obj2 -> ~cl1 - does not do anything - the delete will take care of triggering the destructor correctly.

    0 讨论(0)
  • 2021-01-03 12:38

    obj2 -> ~cl1 ;

    Don't do this! Use delete obj2; instead.

    Addendum
    What you were trying to do was to call the destructor explicitly. Your code does not do that. Your code is getting the address of the destructor and then dropping it into the bit bucket. Your code is a no-op. The correct way to explicitly call the destructor is obj2->~cli();.

    Explicitly calling the destructor is usually something you should not do.

    What you should do is to delete the memory created by new. The correct way to do that is to use the delete operator. This automatically calls the destructor and releases the memory. The destructor does not release the memory. Failing to use delete results in a memory leak.

    0 讨论(0)
  • 2021-01-03 12:38

    First of all you should use delete operator to destrory an object and not call its destructor directtly. Secondly, by doing new you are telling the compiler that you dont want to delete the object when it goes out of the scope. In such case you need to explictly fo delete objj2; to delete the object.

    0 讨论(0)
提交回复
热议问题