CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    13

    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
    Location
    Florida
    Posts
    12,635

    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,449

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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
    576

    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
    576

    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 07: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
  •  





Click Here to Expand Forum to Full Width

Featured