Thread: mtrace shows memory not freed , seems malloc memory leaks

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    mtrace shows memory not freed , seems malloc memory leaks

    Hi guys
    had written cs_malloc to encapsulates malloc , the result would be automatically filled with the new memory position or NULL on failure . but seems there is some bug in my cs_malloc function , which cause memory leaks , mtrace also confirmed this memory leaks ,
    Code:
    oscam-simples.c : 636
     *tmp = malloc (size);
    would highly appreciated if any expert help me , to correct my cs_malloc function inorder to fix this memory leaks (since it's few lines of code (3-4 lines) would appreciated if write the source code correction u mean for this fix)

    P.S : in bellow , i qoute neccassary source codes , but if u need to see furthore source code of the application , u could see it here : trunk (oscam-simples.c is the place where cs_malloc function had written) :


    Code:
    /* This function encapsulates malloc. It automatically  adds an error message to the log if it failed and calls  cs_exit(quiterror) if quiterror > -1. 
       result will be automatically filled with the new memory position or NULL on failure. */
    
    void *cs_malloc(void *result, size_t size, int32_t quiterror){
        void **tmp = result;
    
    // This is oscam-simples.c , line 636 which is indicate in mtrace log
        *tmp = malloc (size);
        if(*tmp == NULL){
            cs_log("Couldn't allocate memory (errno=%d %s)!", errno, strerror(errno));
            if(quiterror > -1) cs_exit(quiterror);
        } else {
            memset(*tmp, 0, size);
    
        }
             
        return *tmp;
    }
    in differnet part of my app modules , i had used cs_malloc , ie , like this :

    Code:
      void *d;
        if(!cs_malloc(&d, n, -1)) return -1;


    Code:
    mtrace log :
    
    $* is no longer supported at /opt/STM/STLinux-2.3/host/bin/mtrace line 2.
    - 0x2c200468 Free 328 was never alloc'd /home/pooya/DEV/stable/mtrace/oscam.c:3137
    
    Memory not freed:
    -----------------
       Address     Size     Caller
    0x004c1378     0x1c  at 0x29561708
    0x004c1398     0x60  at 0x2956370c
    0x004c1400      0x5  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c1410     0x28  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c1440     0x13  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c1458      0xf  at 0x29814592
    0x004c1470      0xc  at 0x298202e2
    0x004c1480      0xc  at 0x298202e2
    0x004c1490      0xd  at 0x298202e2
    0x004c14a8      0xd  at 0x298202e2
    0x004c14c0      0xc  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c14d0      0xc  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c14e0      0x8  at 0x2985e926
    0x004c14f0     0x10  at 0x29850b74
    0x004c1508      0x8  at 0x2985e11a
    0x004c1518      0xc  at 0x2985e176
    0x004c1528     0x17  at 0x2955cc18
    0x004c1548     0x17  at 0x2955fed0
    0x004c1570      0xf  at 0x2985ea2c
    0x004c1588     0x26  at 0x2985e472
    0x004c15b8      0xe  at 0x2985ea2c
    0x004c15d0     0x26  at 0x2985e472
    0x004c1600      0xf  at 0x2985ea2c
    0x004c1618     0x26  at 0x2985e472
    0x004c1648      0xe  at 0x2985ea2c
    0x004c1660     0x26  at 0x2985e472
    0x004c1690     0x24  at 0x2985e472
    0x004c16b8     0x11  at 0x2985ea2c
    0x004c16d0     0x26  at 0x2985e472
    0x004c1700     0x24  at 0x2985e472
    0x004c1728     0x12  at 0x2985ea2c
    0x004c1740     0x26  at 0x2985e472
    0x004c1770     0x11  at 0x2985ea2c
    0x004c1788     0x26  at 0x2985e472
    0x004c17b8      0xf  at 0x2985ea2c
    0x004c17d0     0x26  at 0x2985e472
    0x004c1800      0xc  at 0x2985ea2c
    0x004c1810     0x26  at 0x2985e472
    0x004c1840     0x11  at 0x2985ea2c
    0x004c1858     0x26  at 0x2985e472
    0x004c1888    0x26a  at 0x2955fd8c
    0x004c1af8   0x17d8  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c32d8   0x1338  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c4618    0x160  at 0x29802e36
    0x004c4780     0x88  at 0x29564c06
    0x004c4810     0x88  at 0x29564c06
    0x004c48a0     0x28  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c48d0     0x10  at 0x29850b74
    0x004c48e8    0x10c  at 0x298213a4
    0x004c49f8   0x2010  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c6a10     0x28  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c6a40     0x20  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    0x004c6a68   0x2010  at /home/pooya/DEV/stable/mtrace/oscam-simples.c:636
    ..
    ...
    ....
    maybe it's redundant to this issue , but i also quote the way , i had implemented cs_reallo function too

    Code:
    /* This function encapsulates realloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1.
        result will be automatically filled with the new memory position or NULL on failure. If a failure occured, the existing memory in result will be freed. */
    void *cs_realloc(void *result, size_t size, int32_t quiterror){
    
        void **tmp = (void *)result, **tmp2 = (void *)result;
        *tmp = realloc (*tmp, size);
        if(*tmp == NULL){
            cs_log("Couldn't allocate memory (errno=%d %s)!", errno, strerror(errno));
            free(*tmp2);
            if(quiterror > -1) cs_exit(quiterror);
        }
    
        return *tmp;
    }
    Thanks in advance
    Last edited by jimycn; 10-15-2012 at 03:42 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Where are you freeing the allocated memory? [var]free(tmp);[/var]
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Quote Originally Posted by Click_here View Post
    Where are you freeing the allocated memory? [var]free(tmp);[/var]
    Well , i think nowhere , i knew , this might be the case , but the problem is i really don't know where i have to free(tmp) , in the first thread , i made a link of entire trunk source code for this app (which make it possible for anyone who are interested to take more deep look in entire source code)

    since i had return (*tmp) in cs_malloc , i think , i shouldn't free (tmp) before that return (in the same function(cs_malloc ) am i right?
    could u please fix this bug, i really have no idea , how to fix it...
    Thanks in advance
    Last edited by jimycn; 10-15-2012 at 03:58 PM.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You don't have to free it in that function - but you should free it before the program finishes to avoid the memory leak.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Did you just add that free to your code in post 1? You should make a new post with your new code, that way all the following posts still make sense.

    Don't dereference your pointer when freeing an allocation.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Quote Originally Posted by Click_here View Post
    You don't have to free it in that function - but you should free it before the program finishes to avoid the memory leak.
    Where i should add the free , in which module (oscam-simples.c or after all the lines i had called cs_malloc in all modules?! or in oscam.c , function: main() ? ) , if i would write it in any other functions ( free (tmp) ) then i would get compile error , ( i think the best way is free (tmp) in the cs_malloc function ... would appreciate if tell me where and how should i (free (tmp) ) ?

    I think , it's only one line of code , so easily could write me that one line (and also tell me where i should add it)
    Regards
    Last edited by jimycn; 10-15-2012 at 04:21 PM.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The best time to free it is where it is no longer needed.

    You can free the allocated memory in another function by passing a pointer and then using the pointer for the free statement

    Here is a very basic example which I think that you can work from.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    static int *create_p(void);
    static void distroy_p(int *p);
    
    
    int main(void)
    {
        int *p;
    
    
        p = create_p();
    
    
        if (!p)
        {
            fputs("Memory allocation fail", stderr);
            return EXIT_FAILURE;
        }
    
    
        *p = 42;
    
    
        printf("Answer is %d", *p);
    
    
        distroy_p(p);
    
    
        return EXIT_SUCCESS;
    }
    
    
    static int *create_p(void)
    {
        int *p = malloc(sizeof(int));
        return p;
    }
    
    
    static void distroy_p(int *p)
    {
        free(p);
    }
    Your link to your code is not working for me, but I'm sure if you think about when you no longer need your variable, you can work it out yourself
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc() and memory leaks
    By xaykogeki in forum C Programming
    Replies: 5
    Last Post: 05-01-2010, 11:18 AM
  2. Memory freed at the end of function?
    By walkingbeard in forum C Programming
    Replies: 5
    Last Post: 12-03-2009, 12:26 AM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. shared memory not getting freed
    By Elkvis in forum Linux Programming
    Replies: 19
    Last Post: 02-29-2008, 04:48 PM
  5. Memory leaks and bad indeces: use of malloc
    By f97tosc in forum C Programming
    Replies: 11
    Last Post: 03-25-2003, 09:26 PM

Tags for this Thread