Thread: Is this a memory leak?

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Is this a memory leak?

    Well I'm not sure if this is a memory leak or not. I'm pretty certain it is, but need the view of a more experienced programmer .

    Code:
    // for example 
    char* get_buffer()
    {
    	char* buffer = malloc(20 * sizeof(char));
    	// etc
    	return buffer;	// memory leak?
    }
    At first glance (to me at least) it looks like a memory leak because the buffer can apparently not be free'd.

    How would I get around this without the use of gloabal variables?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to make sure your caller frees it.

    Code:
    getbuffer();
    getbuffer();
    getbuffer();
    getbuffer();
    getbuffer();
    getbuffer();
    getbuffer();
    getbuffer();
    This is a memory leak.





    Code:
    printf( "%s", getbuffer() );
    This is a memory leak as well.






    Code:
    char* lpszText = NULL;
    
    lpszText = getbuffer();
    
    // do something with lpszText
    
    if( lpszText != NULL ) 
    {
         free( lpszText );
         lpszText = NULL;
    }
    This is not.



    We have a ton of functions like this. It's easy to make mistakes using them, but they are better than the alternative of either strings ( sl-o-o-o-w for 100% text-processing applications ) or fixed size arrays ( always either too small or way too large ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I've got it thanks for your help
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Hi,

    I am pretty new to C++. What I think about the code is that the "get_buffer" function returns nothing. Because the pointer is a local variable of the function it will be freed as soon as it goes out of scope (that is the end of the "get_buffer" function call). Am I getting some(serious) things wrong here?

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Quote Originally Posted by sparks
    Hi,

    I am pretty new to C++. What I think about the code is that the "get_buffer" function returns nothing. Because the pointer is a local variable of the function it will be freed as soon as it goes out of scope (that is the end of the "get_buffer" function call). Am I getting some(serious) things wrong here?
    Damn! Beat me to it. Yeah, never return a pointer...

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    There are circumstances where you will return a pointer to something. It is quite legal to do so and I do it in applications.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Returning a pointer from a function is fine... if the memory it points to is either that of a static object or dynamically allocated and not simply local to the function (and if dynamically allocated you should make sure the caller free/deletes the memory).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Quote Originally Posted by Berticus
    Damn! Beat me to it. Yeah, never return a pointer...
    So what's really the problem to that piece of code ? Are we saying the "Super Moderator" didn't point out the right problem and I got it right ? I really wouldn't expect that as a freshman to C++ though.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by sparks
    Hi,

    I am pretty new to C++. What I think about the code is that the "get_buffer" function returns nothing. Because the pointer is a local variable of the function it will be freed as soon as it goes out of scope (that is the end of the "get_buffer" function call). Am I getting some(serious) things wrong here?
    This is incorrect. The pointer is returned by value, meaning a copy of the pointer's value is returned and stored in whatever variable (if any) accepts it in the calling code. The pointer points at dynamic memory, which is not local to the function. So the dynamic memory still exists at the location stored by the pointer, even after the function exits. The allocated memory is still valid, and the location of that memory stored in the pointer can be returned from the function.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Quote Originally Posted by Daved
    This is incorrect. The pointer is returned by value, meaning a copy of the pointer's value is returned and stored in whatever variable (if any) accepts it in the calling code. The pointer points at dynamic memory, which is not local to the function. So the dynamic memory still exists at the location stored by the pointer, even after the function exits. The allocated memory is still valid, and the location of that memory stored in the pointer can be returned from the function.
    Thanks Daved for correcting me. I got the idea completely wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM