locked
Memory leak RRS feed

  • Question

  • Hi,

    Is there a memory leak in below code, if yes could anyone please let me know how to free it.

    int Fun(int x)

    {

     int* ptr = new int[10];

    if(x==0) throw;

    else

     delete []ptr;

    }

    int main()

    {

      try{

    Fun(0);

    }

    catch(...){}

    }

    Tuesday, August 5, 2014 6:59 AM

Answers

  • Hi Dave,

    Thanks for pointing the better way of handling this.

    But in my project the above way of coding is at so many places. So its very difficult to replace all by vector<type> in a quick period of time.

    So i wanted to know, is there any memory leak, i believe that stack unwinding will take care the memory. Please correct me if i am wrong

    Thanks

    I realize your code is probably just an example, but it does not make any sense. At the very least, sample code should compile and actually do something.

    1. You cannot just throw; you must throw something.

    2. If you are throwing when x == 0, why not do it before the memory allocation?

    3. A small array of fixed size is better put on the stack

    int arr[10]; // no possibility of leak

    If the size of the array is only known at run time, then use vector<int> as others have noted.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by LokanathNayak Tuesday, August 12, 2014 4:47 AM
    Tuesday, August 5, 2014 10:32 AM

All replies

  • >Is there a memory leak in below code, if yes could anyone please let me know how to free it.

    The best way is to never have any calls to new, delete, malloc, free
    in your code.

    In your example, use an STL vector<int>.

    Dave

    Tuesday, August 5, 2014 7:34 AM
  • Hi Dave,

    Thanks for pointing the better way of handling this.

    But in my project the above way of coding is at so many places. So its very difficult to replace all by vector<type> in a quick period of time.

    So i wanted to know, is there any memory leak, i believe that stack unwinding will take care the memory. Please correct me if i am wrong

    Thanks

    Tuesday, August 5, 2014 8:15 AM
  • Because of the new, that will leak memory as the stack unwinding will only free stack variables. The new operator creates the array on the free store and stores the pointer on the stack.

    The two ways to make sure there is no memory leak is to either use an existing STL class, as pointed out, or to wrap the memory allocation in a class itself and create that on the stack. Since the stack unwinding is guaranteed to call the destructor of any classes on the stack, if you free this memory in the destructor then you will plug the leak. There are two great classes in C++ that help you do this already, std::shared_ptr and std::unique_ptr. These are smart pointers that are designed to automatically clean up pointers that go out of scope.

    The not so satisfying way would be to wrap the entire function in a try/catch block. If it does throw then free the memory in the catch statement and re-throw the exception. Using a stack based class is much cleaner and less error prone.


    This is a signature Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.



    • Edited by Darran Rowe Tuesday, August 5, 2014 8:41 AM
    Tuesday, August 5, 2014 8:30 AM
  • Hi Dave,

    Thanks for pointing the better way of handling this.

    But in my project the above way of coding is at so many places. So its very difficult to replace all by vector<type> in a quick period of time.

    So i wanted to know, is there any memory leak, i believe that stack unwinding will take care the memory. Please correct me if i am wrong

    Thanks

    I realize your code is probably just an example, but it does not make any sense. At the very least, sample code should compile and actually do something.

    1. You cannot just throw; you must throw something.

    2. If you are throwing when x == 0, why not do it before the memory allocation?

    3. A small array of fixed size is better put on the stack

    int arr[10]; // no possibility of leak

    If the size of the array is only known at run time, then use vector<int> as others have noted.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by LokanathNayak Tuesday, August 12, 2014 4:47 AM
    Tuesday, August 5, 2014 10:32 AM
  • On 8/5/2014 2:59 AM, LokanathNayak wrote:

    int Fun(int x)
    {
    if(x==0) throw;

    "throw" without an argument, executed outside of an exception handler, results in std::terminate (and eventually abort() ) being called. In other words, the program is terminated abnormally; a memory leak is the least of your worries.


    Igor Tandetnik
    Tuesday, August 5, 2014 3:10 PM