Ways to avoid memory leak when exception thrown

chihyang

This is an exercise from C++ Primer, chapter 18:

void exercise(int *b, int *e)
{
    vector<int> v(b, e);
    int *p = new int[v.size()];
    ifstream in("ints");
    // exception occurs here
}

The code above would cause memory leak, because the memory we manage directly (i.e. p) is not automatically freed when an exception occurs.

Exercise 18.3:

There are two ways to make the previous code work correctly if an exception is thrown. Describe them and implement them.

I know we can use smart pointer to avoid this pitfall:

void exercise(int *b, int *e)
{
    vector<int> v(b, e);
    unique_ptr<int[]> p(new int[v.size()]);
    ifstream in("ints");
    // exception occurs here
}

or:

void exercise(int *b, int *e)
{
    vector<int> v(b, e);
    shared_ptr<int> p(new int[v.size()], [](int *p){ delete[] p; });
    ifstream in("ints");
    // exception occurs here
}

I'm not sure if these are TWO ways. After all, they are the same virtually. So I thought of another way:

void exercise(int *b, int *e)
{
    vector<int> v(b, e);
    int *p = new int[v.size()];
    ifstream in("ints");
    // exception occurs here
    if(!in)
        throw p;
}

// caller
try {
    exercise(b, e);
} catch (int *err) {
    delete[] err; // initialize err with b and delete e.
}

If exception occurs, throw p to initialize another pointer and delete that one. I know this is not a perfect solution, because other exceptions might occur so that I don't even have a chance to throw p. But I can't think of a better one. Could you help to find the second way?

John Zwinck

You can explicitly catch and re-throw the exception:

void exercise(int *b, int *e)
{
    vector<int> v(b, e);
    int *p = new int[v.size()];
    try {
        ifstream in("ints");
        // exception occurs here
    } catch (...) {
        delete[] p;
        throw;
    }
    delete[] p;
}

Of course this doesn't work so well if you have multiple resources to free, because you may end up with nested try/catch blocks. Not to mention that you have two deletes for one new. So RAII is preferred.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Will this method cause a memory leak when it throws an exception?

From Dev

Will this method cause a memory leak when it throws an exception?

From Dev

Avoid Memory leak

From Dev

How to avoid this memory leak?

From Dev

Avoid memory leak with WeakReference Android

From Dev

When is "throws Exception" thrown?

From Dev

How to release memory in android to avoid memory leak

From Dev

OracleBulkCopy Memory Leak(OutOfMemory Exception)

From Dev

How to avoid memory leak when passing function-returned pointer as input to other function?

From Dev

nodejs setMaxListeners to avoid memory leak detection

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

weak to non class types to avoid memory leak

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How can I fix to avoid a memory leak?

From Dev

How can I avoid a memory leak in this function?

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

Is setting ulimit -v sufficient to avoid memory leak

From Dev

What happens when an exception is thrown?

From Dev

Exception thrown when calling getFriends

From Dev

Memory leak when using DnsGetCacheDataTable

From Dev

Vulkan: Memory leak when rendering

From Dev

Memory leak when using WKScriptMessageHandler

From Dev

Memory leak when using MBProgressHUD

From Dev

Memory leak when using DnsGetCacheDataTable

From Dev

View Exception when stopped on Exception Thrown

From Dev

C++ throwing exception causes memory leak

From Dev

Exception has been thrown by the target of an invocation thrown when scaffolding a controller

From Dev

Is it possible to automatically break into the debugger when a exception is thrown?

From Dev

Is it possible to check cases when exception is thrown with QuickCheck?

Related Related

  1. 1

    Will this method cause a memory leak when it throws an exception?

  2. 2

    Will this method cause a memory leak when it throws an exception?

  3. 3

    Avoid Memory leak

  4. 4

    How to avoid this memory leak?

  5. 5

    Avoid memory leak with WeakReference Android

  6. 6

    When is "throws Exception" thrown?

  7. 7

    How to release memory in android to avoid memory leak

  8. 8

    OracleBulkCopy Memory Leak(OutOfMemory Exception)

  9. 9

    How to avoid memory leak when passing function-returned pointer as input to other function?

  10. 10

    nodejs setMaxListeners to avoid memory leak detection

  11. 11

    How to avoid a memory leak with __block and completion blocks

  12. 12

    weak to non class types to avoid memory leak

  13. 13

    how to avoid memory leak in dynamically allocated widget

  14. 14

    How can I fix to avoid a memory leak?

  15. 15

    How can I avoid a memory leak in this function?

  16. 16

    How to avoid a memory leak with __block and completion blocks

  17. 17

    Is setting ulimit -v sufficient to avoid memory leak

  18. 18

    What happens when an exception is thrown?

  19. 19

    Exception thrown when calling getFriends

  20. 20

    Memory leak when using DnsGetCacheDataTable

  21. 21

    Vulkan: Memory leak when rendering

  22. 22

    Memory leak when using WKScriptMessageHandler

  23. 23

    Memory leak when using MBProgressHUD

  24. 24

    Memory leak when using DnsGetCacheDataTable

  25. 25

    View Exception when stopped on Exception Thrown

  26. 26

    C++ throwing exception causes memory leak

  27. 27

    Exception has been thrown by the target of an invocation thrown when scaffolding a controller

  28. 28

    Is it possible to automatically break into the debugger when a exception is thrown?

  29. 29

    Is it possible to check cases when exception is thrown with QuickCheck?

HotTag

Archive