Resource leak.. ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Resource leak.. ?

  1. #1
    Join Date
    May 2010
    Posts
    12

    Resource leak.. ?

    #include<iostream>

    int* func_b() {
    int *c = new int;
    return c;
    }

    int main() {
    int* b = func_b();
    return 0;
    }


    I can see that memory leak would be there in above code because I am not freeing the memory allocated using "new".
    Want to know what is the best way to avoid this leak? One solution is:

    int* func_b() {
    int *c = new int;
    return c;
    }

    int main() {
    int* b = func_b();
    delete b;
    return 0;
    }

    Is this optimal solution? I would be great if someone can suggest alternative way.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Resource leak.. ?

    That's a memory leak, not a resource leak. Not the same thing.

    The optimal solution really depends on the context. What you've proposed is okay in that simple example.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Resource leak.. ?

    Quote Originally Posted by hemant.bhargava7 View Post
    Want to know what is the best way to avoid this leak?
    It depends on what you mean by "best way". The program you posted is too trivial to tell you what is best or better. To recommend the "best way" requires us to see the real program you're working on and have you describe the issues you're having when it comes to memory allocation/deallocation.

    However in general, you should attempt to avoid writing code where one function allocates, and another function (or the user of the function) has to remember to deallocate.

    1) What if the allocation scheme changes in func_b()? Then all modules calling func_b() need to change to deallocate properly. This means more maintenance and a possibility of incorrect/mismatch allocation and deallocation methods being called.

    2) And just in general, what if the client/user of func_b() just forgets to call "delete" (or the deallocation function)? A memory leak.

    Writing code like this puts your code more at risk for memory leaks and/or wrong usage. Things such as smart pointers, RAII, etc. reduces the need to use "new" in such a wide-open fashion as what you've posted.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,997

    Re: Resource leak.. ?

    if someone can suggest alternative way
    A common c+ way would be to use 'smart pointers' - auto_ptr in c++98 or unique_ptr in c++11.
    See
    http://www.cplusplus.com/reference/memory/auto_ptr/
    http://www.cplusplus.com/reference/memory/unique_ptr/

    Another common way, as Paul mentioned, is to use RAII (Resource Acquisition Is Initialisation). See
    http://en.wikipedia.org/wiki/Resourc...Initialization
    http://stackoverflow.com/questions/2...alization-raii

    Also, depending upon what you are trying to achieve in practice, if you are using classes then allocate memory in the constructor(s) and free memory in the destructor.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,641

    Re: Resource leak.. ?

    The best way is to avoid returning something that needs to be explicitely cleaned up.
    smart pointers might work as a generic case, but don't work for all cases (if the memory isn't allocated with new)

    The best way is to return an object and handle any allocation/deallocation needs in the implemention and hiding it from the public interface.

  6. #6
    Join Date
    Jul 2013
    Posts
    168

    Re: Resource leak.. ?

    Quote Originally Posted by GCDEF View Post
    That's a memory leak, not a resource leak. Not the same thing.
    A memory leak is a kind of resource leak.

  7. #7
    Join Date
    Jul 2013
    Posts
    168

    Re: Resource leak.. ?

    Quote Originally Posted by hemant.bhargava7 View Post
    Is this optimal solution? I would be great if someone can suggest alternative way.
    The best alternative is to use reference counting smart pointers. There's one called std::shared_ptr which is part of the language (since C++ 11).

    It's used like an ordinary pointer but makes sure the allocated object is deleted automagically when it's not referenced from anywhere anymore. The only principal drawback is that it cannot handle circular references (like for example a circular linked list).

    Another alternative is to use a third-party garbage collector but that's advanced and not very common in C++. The only one you ever hear about really is the Boehm GC. Hopefully the situation will improve now that C++ in under increasing pressure from GC based languages.
    Last edited by razzle; February 4th, 2014 at 06:07 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center