Like Tree2Likes

Memory leak

This is a discussion on Memory leak within the General Discussions forums, part of the Community Boards category; Hi all, Just a common sense question. I've been totally blanked out with this. I'm just thinking such a fool ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Memory leak

    Hi all,

    Just a common sense question. I've been totally blanked out with this. I'm just thinking such a fool i am. If sombody comes along and say I'll give a C code, which you can edit and recompile and run it. How do you check for memory leak in the code? And that should be not using any of the memory managment tools. How would you go on about to check if there was a memory leak?
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,608
    valgrind

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Replace all calls to malloc() with a call to a wrapper function which records the allocation, its size, and location in memory. Other helpful information to record are file name and line number where the allocation occurs (use the __FILE__ and __LINE__ special preprocessor macros). Replace all calls to free() with a call to a wrapper than marks the allocation as being freed. At the end of program execution (or at intermediate times, of your choosing), dump the list of blocks which are still allocated.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Define malloc as a macro.

    main.c
    Code:
    #include "debug.h"
    
    #include <stdlib.h>
    
    #define DEBUG
    
    int main()
    {
       char *test = (char*)malloc(1);
       free(test);
    }
    debug.h
    Code:
    #ifndef DEBUG_H
    #define DEBUG_H
    
    #ifdef DEBUG
    #define malloc(b) debug_malloc(b)
    #define free(m) debug_free(m)
    #endif
    
    void *debug_malloc(size_t);
    void debug_free(void*);
    bool unfreed_memory();
    #endif
    debug.c
    Code:
    #include "debug.h"
    
    #include <stdlib.h>
    
    static int allocs = 0;
    
    void *debug_malloc(size_t bytes)
    {
       allocs++;
       return malloc(bytes);
    }
    
    void debug_free(void *memory)
    {
       allocs--;
       free(memory);
    }
    
    bool unfreed_memory()
    {
       return allocs != 0;
    }
    I'm sure there's a better way, but this works.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by User Name: View Post
    Define malloc as a macro.

    Code:
    #define malloc(b) debug_malloc(b)
    It's probably better to do it this way:

    Code:
    #define malloc debug_malloc
    I'll leave the why as an exercise
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by brewbuck View Post
    It's probably better to do it this way:

    Code:
    #define malloc debug_malloc
    I'll leave the why as an exercise
    Does my way work at all? If it does, IMHO it is better because it leaves the option of automatically including __FILE__ and __LINE__ open. Eg:
    Code:
    #define malloc(b) debug_malloc(b, __FILE__, __LINE__)
    Of course, this could require changes to debug_malloc.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The following code is why it's a problem:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void *my_malloc(size_t b)
    {
    	printf("Allocation of %d bytes\n", b);
    	return malloc(b);
    }
    
    #define malloc(b) my_malloc(b)
    /* #define malloc my_malloc */
    
    int main()
    {
    	void *(*pAllocator)(size_t) = &malloc;
    	
    	pAllocator(128);
    }
    The call through the pointer isn't tracked (but try switching out the macro with the commented-out version). Using a macro to automatically send __FILE__ and __LINE__ to an allocation wrapper isn't a bad idea (in fact it's the only way to get the file and line number automatically), but directly redefining malloc(), or any function really, can be dangerous. Yeah, macros in general are dangerous, but this is C...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Linux: valgrind
    Visual C++: Visual Leak Detector.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    @g4j31a5
    @Yarin
    The answer is wrong. I mentioned not using any memory management tools. No tools should be used.

    And other yes you guys are right. It’s as simple as printing out a debug statement and keep track of how much you allocate and deallocate. This is what employers are looking for. They are looking for a very simple answer and I couldn't think in a way different as put in some debug print. I was thinking ah how could the debug print might be helpful. And I said we might have to step through the code in that case. This is of course a time consuming task. What at the end, I just said "I’m not sure how debug prints, will help". And he went *surprised* YES IT WOULD!!! Why not*. And it all made sense. Perhaps I was thinking in the same context, but my mind wasn't working on giving him a simple solution. But it turned out to be that he was expecting just about that simple answer (debug prints)

    ssharish
    Last edited by ssharish2005; 06-28-2011 at 06:53 AM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    And also need to be watched out for multi-threaded application, which the proposed solution might have to be altered.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    You just need to make sure to synchronize access to malloc in a multi-threaded application. Otherwise it should be fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    What really annoyed me was why I couldn’t answer such a simple approach when it was questioned to me. You know how frustrating it is to just loose an opportunity just because I wasn't able to answer a simple answer :-/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Smile

    Hi
    Code:
     void *debug_malloc(size_t bytes)
    {
       allocs++;
       return malloc(bytes);
    }
    In this code you are trying to increment the alloc value before allocating the actual memory for the pointer which gives wrong statistics when you analyse the debub prints finaly.same for free API also

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Hi Harish..
    What is your comment on this?
    Is this question asked in any interviews?
    what you replied for this?

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    You just need to make sure to synchronize access to malloc in a multi-threaded application. Otherwise it should be fine.
    malloc() isn't guaranteed to be thread-safe, but most modern C runtimes provide a thread-safe malloc() which is much more efficient than simply wrapping all calls to malloc() with a big lock.
    Last edited by brewbuck; 06-28-2011 at 06:23 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  2. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  3. Do I have a memory leak? (SDL)
    By antex in forum Game Programming
    Replies: 3
    Last Post: 03-02-2006, 05:18 PM
  4. Is this a memory leak?
    By jasonhv in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2005, 08:37 PM
  5. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 02:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21