Vulkhan
Vulkhan

Reputation: 458

Does the allocated pointers inside an object are automatically deallocated after an exception is thrown?

Actually i'm using the following way to make sure my buffer is correctly cleaned when an exception is thrown within my object, but it takes a lot more code than the below example.

class Foo
{
private:
    int m_bufferSize;
    char m_isNetBufferSet;
    char* m_netBuffer;

    setBufferSize(const int bufferSize)
    {
        if (!m_isNetBufferSet)
        {
            m_bufferSize = bufferSize;
            m_netBuffer = new char[m_bufferSize];
            ZeroMemory(m_netBuffer, m_bufferSize);
        }
    }

    freeBuffer()
    {
        if (m_isNetBufferSet)
        {// i'm not using m_netBuffer != NULL because i'm not sure how it behave
            delete [] m_netBuffer;
            m_isNetBufferSet    = 0;
            m_bufferSize        = 0;
        }
    }

public:
    Foo()
    {
        m_isNetBufferSet    = 0;
        m_bufferSize        = 0;
        m_netBuffer         = NULL;
    }

    bar()
    {
        std::wstring hello(L"Hey, how are you?");

        freeBuffer();
        setBufferSize(hello.size() * sizeof(wchar_t));

        throw std::runtime_error("Oh noes something suddenly went wrong :(");

        /* never reached... */
    }

    ~Foo()
    {
        freeBuffer();
    }
};

I know local variables and objects are deallocated after an exception, but does the allocated pointers inside an object are automatically deallocated after an exception is thrown? Is it possible to simplify my code by doing the following without creating a memory leak?

class Foo
{
public:
    bar()
    {
        std::wstring hello(L"Hey, how are you?");
        char *netBuffer = new char[hello.size() * sizeof(wchar_t)];

        throw std::runtime_error("Oh noes something suddenly went wrong :(");

        /* never reached... */
    }
};

Upvotes: 1

Views: 73

Answers (2)

eerorika
eerorika

Reputation: 238401

I know local variables and objects are deallocated after an exception

Correct.

but does the allocated pointers inside an object are automatically deallocated after an exception is thrown?

No. But assuming you're talking about a local automatic object that's destroyed due to an exception, then the destructor of the object should be designed so that it deallocates any memory that the object may have allocated.

If that local object is simply a pointer, then the pointer is not inside the object. The pointer is the object. Regular pointers will not deallocate pointed memory when they are destroyed.

Upvotes: 1

Marinos K
Marinos K

Reputation: 1829

When an exception is thrown then the destructor of everything that has been allocated in the stack will be called. Objects that have been created with neware not on the stack but on the heap and WILL NOT BE DELETED implicitly. You need to make sure you delete them yourself.

To have such objects automatically deleted it is advisable to use RAII or some smart pointer such as std::unique_ptr or std::shared_ptr in c++11/14. In your case, however, you may want to consider a std::vector<char> or std::string instead.

Upvotes: 2

Related Questions