Where should we delete object when exception occurred?

Hi All,

Where should we delete ptr object in function Func() ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Base
{
 private:
   int a;
 public:
    Base()
    {
       // Constructor
    }
    ~Base()
    {
      // Destructor
    }
};

void Func()
{
  try()
  {
    Base* ptr = new Base();
    //  code
    // Exception occurred 
    //  code
  }
  catch(exception e)
  {

  }

}
This is why you should follow RAII and not use raw pointers.

To add to keskiverto's response, you should delete it when ptr goes out of scope. Because the function could throw an exception anywhere, you need this to happen automatically.

The best option would be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Func()
{
  try()
  {
    Base base_object;
    //  code
    // Exception occurred 
    //  code
  }
  catch(exception e)
  {

  }
}


If for some reason it must be made on the heap, use a smart pointer that will delete the object when the smart pointer goes out of scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Func()
{
  try()
  {
    std::unique_ptr<Base> = ptr (make_unique<Base>());
    //  code
    // Exception occurred 
    //  code
  }
  catch(exception e)
  {

  }
}


Both of these follow the RAII idiom (which is increasingly badly named these days).
Last edited on
Thanks both of you for reply.
@Repeater
As you said by some reason if we want to make it on heap and still dont use smart pointer.
You mean,
If exception occurred, Then there is not other way to delete this object?
 
Base* ptr = new Base();
I have read about RAII on google, And happy to understand the concept.
Below post found very clear on RAII:

"RAII is the design paradigm to ensure that variables handle all needed initialization in their constructors and all needed cleanup in their destructors. This reduces all initialization and cleanup to a single step.

C++ does not require RAII, but it is increasingly accepted that using RAII methods will produce more robust code.

The reason that RAII is useful in C++ is that C++ intrinsically manages the creation and destruction of variables as they enter and leave scope, whether through normal code flow or through stack unwinding triggered by an exception. That's a freebie in C++.

By tying all initialization and cleanup to these mechanisms, you are ensured that C++ will take care of this work for you as well.

Talking about RAII in C++ usually leads to the discussion of smart pointers, because pointers are particularly fragile when it comes to cleanup. When managing heap-allocated memory acquired from malloc or new, it is usually the responsibility of the programmer to free or delete that memory before the pointer is destroyed. Smart pointers will use the RAII philosophy to ensure that heap allocated objects are destroyed any time the pointer variable is destroyed."
If exception occurred, Then there is not other way to delete this object?


If you leave that scope by exception, you've lost the pointer that was created inside the scope. Without the pointer, you cannot delete it.
@Repeater
Yeh I got it.

Thanks.
Topic archived. No new replies allowed.