0

Is there any Memory leak in this program if yes then how to overcome this problem??

#include<iostream>
using namespace std;
class A
{
    double a,b;
  public:
    A(double X,double Y)
    {
        try
        {
        a = X;
        b = Y;
            if(b==0)
            throw b;
            else
            cout <<"Division:"<<a/b<<endl;
        }
        catch (double z)
        {
            throw z;
        }
    }
      ~A()
        {
            cout<<"Object Destroyed"<<endl;
         }
 }; 
 int main()
 {
   try
   {
    A object(10,2);
    A object1(4,2);
    A object2(4,0);
   }
  catch(double a)
  {
    cout<<"Zero:"<<a<<endl;
  }
  return 0;
  }

For Object2 why destructor is not called,and what is the best way to throw exception from constructor if there is any??

Rose Ahmed
  • 41
  • 6
  • 7
    I don't see any memory leak here. – Fred Larson Jul 13 '20 at 15:20
  • 5
    object2 is never constructed, so there is nothing to destruct – rustyx Jul 13 '20 at 15:22
  • why destructor is not called for object2?? – Rose Ahmed Jul 13 '20 at 15:22
  • 3
    `catch (double z) { throw z; }` seems rather pointless. Why have the `try` / `catch` at all if this is all you do with it? – Jesper Juhl Jul 13 '20 at 15:22
  • 2
    @RoseAhmed "why destructor is not called for object2??" - Because the object was never fully constructed, the constructor threw an exception, so there is no object on which to call a destructor. – Jesper Juhl Jul 13 '20 at 15:24
  • ya i know that is pointless for this program but if i allocate memory dynamically for same program first i have to delete the memory allocated dynamically in catch block inside the constructor. Anyway my question was for object1 why destructor is not called?? – Rose Ahmed Jul 13 '20 at 15:33
  • 1
    @RoseAhmed: `object` and `object1` would be fully constructed and destructed. `object2` would not have completed construction, due to the exception, so the destructor is not called. There's no memory leak though. `object2` could risk a memory leak, but the solution there is to use RAII members, and don't use `new` yourself. – Mooing Duck Jul 13 '20 at 15:36
  • 1
    @RoseAhmed You don't need explicit `try` / `catch` to deal with cleaning up dynamically allocated memory. All you need is to use containers or smart pointers (rather than raw `new` and `delete` - which you should almost *never* use) that clean up after themselves. – Jesper Juhl Jul 13 '20 at 15:36
  • 1
    *but if i allocate memory dynamically for same program* -- Move the allocation to another object that does not `throw` and use that in your class, for example a container, smart pointer, etc. – PaulMcKenzie Jul 13 '20 at 15:36
  • but what about memory allocated for variables present in object which is not fully constructed?? – Rose Ahmed Jul 13 '20 at 15:38
  • 1
    "but what about memory allocated for variables present in object which is not fully constructed?" - already initialized members will be properly destroyed when the exception is thrown, so your `a` and `b` members don't leak. – Jesper Juhl Jul 13 '20 at 15:42
  • @JesperJuhl Thank you. – Rose Ahmed Jul 13 '20 at 16:01

1 Answers1

1

So you are not doing any dynamic allocation, it is all stack based. So it will clean up acceptably. As you are aware, your destructor is not doing anything useful.

But note that throw in a constructor does not call the corresponding destructor, and if that constructor for object2 /had/ done any allocation (that you wanted to clean up in the destructor) before it threw then it could very easily leak. But note that members with constructors and base classes to get destructed. The rule is that for every subobject whose construction was /completed/ the corresponding destructor will get called, but by throwing in the middle of this constructor function, then it did not complete, and its corresponding destructor is not called.

This is the difficulty of using throw in constructors - and the need to either bullet-proof everything with RAII buffers or clean up in the catch.

There is more info here about what does and does not get destructed: What destructors are run when the constructor throws an exception?

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27