Deleting an object in C++

前端 未结 6 1871
清酒与你
清酒与你 2021-02-01 03:56

Here is a sample code that I have:

void test()
{
   Object1 *obj = new Object1();
   .
   .
   .
   delete obj;
}

I run it in Visual Studio, an

相关标签:
6条回答
  • 2021-02-01 04:14

    saveLeaves(vec,msh);
    I'm assuming takes the msh pointer and puts it inside of vec. Since msh is just a pointer to the memory, if you delete it, it will also get deleted inside of the vector.

    0 讨论(0)
  • 2021-02-01 04:16

    Isn't this the normal way to free the memory associated with an object?

    This is a common way of managing dynamically allocated memory, but it's not a good way to do so. This sort of code is brittle because it is not exception-safe: if an exception is thrown between when you create the object and when you delete it, you will leak that object.

    It is far better to use a smart pointer container, which you can use to get scope-bound resource management (it's more commonly called resource acquisition is initialization, or RAII).

    As an example of automatic resource management:

    void test()
    {
        std::auto_ptr<Object1> obj1(new Object1);
    
    } // The object is automatically deleted when the scope ends.
    

    Depending on your use case, auto_ptr might not provide the semantics you need. In that case, you can consider using shared_ptr.

    As for why your program crashes when you delete the object, you have not given sufficient code for anyone to be able to answer that question with any certainty.

    0 讨论(0)
  • 2021-02-01 04:20

    Just an update of James' answer.

    Isn't this the normal way to free the memory associated with an object?

    Yes. It is the normal way to free memory. But new/delete operator always leads to memory leak problem.

    Since c++17 already removed auto_ptr auto_ptr. I suggest shared_ptr or unique_ptr to handle the memory problems.

    void test()
    {
        std::shared_ptr<Object1> obj1(new Object1);
    
    } // The object is automatically deleted when the scope ends or reference counting reduces to 0.
    
    • The reason for removing auto_ptr is that auto_ptr is not stable in case of coping semantics
    • If you are sure about no coping happening during the scope, a unique_ptr is suggested.
    • If there is a circular reference between the pointers, I suggest having a look at weak_ptr.
    0 讨论(0)
  • 2021-02-01 04:22

    if it crashes on the delete line then you have almost certainly somehow corrupted the heap. We would need to see more code to diagnose the problem since the example you presented has no errors.

    Perhaps you have a buffer overflow on the heap which corrupted the heap structures or even something as simple as a "double free" (or in the c++ case "double delete").

    Also, as The Fuzz noted, you may have an error in your destructor as well.

    And yes, it is completely normal and expected for delete to invoke the destructor, that is in fact one of its two purposes (call destructor then free memory).

    0 讨论(0)
  • 2021-02-01 04:24

    Isn't this the normal way to free the memory associated with an object?

    Yes, it is.

    I realized that it automatically invokes the destructor... is this normal?

    Yes

    Make sure that you did not double delete your object.

    0 讨论(0)
  • 2021-02-01 04:30

    Your code is indeed using the normal way to create and delete a dynamic object. Yes, it's perfectly normal (and indeed guaranteed by the language standard!) that delete will call the object's destructor, just like new has to invoke the constructor.

    If you weren't instantiating Object1 directly but some subclass thereof, I'd remind you that any class intended to be inherited from must have a virtual destructor (so that the correct subclass's destructor can be invoked in cases analogous to this one) -- but if your sample code is indeed representative of your actual code, this cannot be your current problem -- must be something else, maybe in the destructor code you're not showing us, or some heap-corruption in the code you're not showing within that function or the ones it calls...?

    BTW, if you're always going to delete the object just before you exit the function which instantiates it, there's no point in making that object dynamic -- just declare it as a local (storage class auto, as is the default) variable of said function!

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