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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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