0

I know auto_ptr destruct object automatically when it is out of scope. But when Constructor throws exception, it does not destruct object(take a look at below code snippet).

Let me explain below code , I have class A which has an auto_ptr field "autoPtr" (which has a pointer to Test class). In below scenario, A class constructor throws "Malloc" error. I am expecting Auto_Ptr to call Test class destructor but it does not.

#include <iostream>
#include <memory>
using namespace std;
class Test
{
public:
    Test()
    {
        cout << "Test Constructor" << endl;
    }
    ~Test()
    {
        cout << "Test Destructor" << endl;
    }
};

class A
{
private:
    int *ptr;
    auto_ptr<Test> autoPtr;
public:
    A():autoPtr(new Test()), ptr(NULL)
    {
        ptr = (int *)malloc(INT_MAX);
        if(ptr == NULL)
            throw exception("Malloc failed");
    }
};

int main()
{
    try
    {
        A *obj = new A();
    }
    catch (exception e)
    {
        cout << "Caught exception :" <<e.what()<< endl;
    }
}

Output is :

Test Constructor

Caught exception :Malloc failed


Why Test class destructor was not called in the above code ?

Also Who frees A class object (obj) memory ?

siva
  • 1,693
  • 2
  • 21
  • 29
  • Please update your question with a complete compileable example of the problem. This code does not compile. – Richard Hodges Oct 20 '15 at 08:22
  • 3
    Which compiler you use? http://melpon.org/wandbox/permlink/wuD6nXGVyY33R39l works as expected. – ForEveR Oct 20 '15 at 08:22
  • I am using Visual Studio 2015 – siva Oct 20 '15 at 08:27
  • Same result with vc: http://rextester.com/ZPZO52891 Are you sure, that your code is compiled and not old version is used? – ForEveR Oct 20 '15 at 08:29
  • For your second question: The compiler will free the allocated memory if the constructor throws. Then the `obj` is never assigned a value. – Bo Persson Oct 20 '15 at 08:43
  • @ForEveR, usually I compile c++ code in "Visual Studio Developed Command prompt" (it does not need visual studio project, it just need single cpp file) there I received wrong output. Now I have created c++ project, now I am getting correct output as expected. My main problem got solved. Do you know who frees A class object memory ? (in the above code) – siva Oct 20 '15 at 08:44
  • @Siva The destructor of `A` isn't called, because allocated the instance dynamically `A *obj = new A();` and you never call `delete obj;` – πάντα ῥεῖ Oct 20 '15 at 08:47
  • 3
    Why are you using `auto_ptr` and not `unique_ptr` ? – Jonathan Potter Oct 20 '15 at 08:48
  • @πάνταῥεῖ, I understood why destructor is not called. But some one has to free allocated memory ? right ?. A *obj= new A() is two step process 1) memory allocation for object 2) object initialization through constructor. – siva Oct 20 '15 at 08:54
  • @JonathanPotter, I am not familiar with unique_ptr. I will learn about it. Thanks for advice – siva Oct 20 '15 at 08:57
  • @siva _"But some one has to free allocated memory ?"_ It's you who's in charge to do so. – πάντα ῥεῖ Oct 20 '15 at 08:57

0 Answers0