Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of memory leak in c++ (by use of exceptions) [duplicate]

In C++ How to program there is a paragraph that say:

A common programming practice is to allocate dynamic memory, assign the address of that memory to a pointer, use the pointer to manipulate the memory and deallocate the memory with delete when the memory is no longer needed. If an exception occurs after successful memory allocation but before the delete statement executes, a memory leak could occur. The C++ standard provides class template unique_ptr in header to deal with this situation.

Any on could introduce me a real example that exception occur and memory will leak like this post?

like image 646
Milad Khajavi Avatar asked Mar 12 '13 21:03

Milad Khajavi


People also ask

Which of the following is an example of memory leak?

An example of memory leak The memory leak would occur if the floor number requested is the same floor that the elevator is on; the condition for releasing the memory would be skipped. Each time this case occurs, more memory is leaked.

Can exceptions cause memory leaks?

The pointer to the object goes out of scope when the exception leaves the function, and the memory occupied by the object will never be recovered as long as the program is running. This is a memory leak; it would be detected by using the memory diagnostics.

How are memory leaks caused in C?

Memory leaks occur when new memory is allocated dynamically and never deallocated. In C programs, new memory is allocated by the malloc or calloc functions, and deallocated by the free function.


2 Answers

A bit more subtle example.

Take an naive implementation of a class that holds two dynamically allocated arrays:

struct Foo {
private:
    int* first;
    int* second;
public:
    Foo()
        : first(new int[10000])
        , second(new int[10000])
    { }

    void Bar() { throw 42; }

    ~Foo()
    {
        delete [] first;
        delete [] second;
    }
};

int main()
{
    Foo f;
    /* more code */
}

Now, if we get an exception because we call method Bar somewhere, everything's fine - the stack unwinding guarantess that f's destructor gets called.

But if we get a bad_alloc when initializing second, we leak the memory that first points to.

like image 94
jrok Avatar answered Sep 30 '22 18:09

jrok


class MyClass
{
public:
    char* buffer;
    MyClass(bool throwException)
    {
        buffer = new char[1024];
        if(throwException)
            throw std::runtime_error("MyClass::MyClass() failed");
    }

    ~MyClass()
    {
        delete[] buffer;
    }
};

int main()
{
    // Memory leak, if an exception is thrown before a delete
    MyClass* ptr = new MyClass(false);
    throw std::runtime_error("<any error>");
    delete ptr;
}

int main()
{
    // Memory leak due to a missing call to MyClass()::~MyClass()
    // in case MyClass()::MyClass() throws an exception.
    MyClass instance = MyClass(true);
}

See also: C++ : handle resources if constructors may throw exceptions (Reference to FAQ 17.4]

like image 29
Sam Avatar answered Sep 30 '22 19:09

Sam