Like Tree1Likes
  • 1 Post By Salem

malloc , how could free a return value?

This is a discussion on malloc , how could free a return value? within the C Programming forums, part of the General Programming Boards category; my app is use in stlinux (sh4) and unfortunately valgrind does not support sh4 cpu . since i saw memory ...

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

    malloc , how could free a return value?

    my app is use in stlinux (sh4) and unfortunately valgrind does not support sh4 cpu .
    since i saw memory leak with my app , i had used mtrace , and it confirmed that some memory is not free . the problem is , variable of malloc used in the return , therefore i do not have any idea , how could i free it (since if it would be free , then returning in the functions is meaningless)
    For take a deeper look deeper in entire source code , please check this trac browser :

    i had written cs_malloc (put bellow code from oscam-simple.c in above link) , mtrace log says , that in line :

    Code:
    *tmp = malloc (size);
    memory is not free

    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;
            *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;
        }
    and then for malloc , i call it , like this :
    Code:
          // create the AES key entry for the linked list
            if(!cs_malloc(&new_entry, sizeof(AES_ENTRY), -1)) return;
    please take a look at these 3 functions (which malloc is not free , and as other users said , valgrind claim that these codes cause memory leaks cause by these 3 functions module-datastruct-llist.c

    the memory leaks cause by 3 different parts :

    1. in bellow codes "new" would never free , but since it use in return of that function , i don't have idea , how could i free it

    Code:
        LL_NODE* ll_append_nolock(LLIST *l, void *obj)
        {
            if (l && obj) {
                LL_NODE *new;
                if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
                new->obj = obj;
                
                if (l->last)
                    l->last->nxt = new;
                else
                    l->initial = new;
                l->last = new;    
                
                l->count++;
                return new;
                }
            }

    2. also "l" use in bellow function , again since it use in return function , i have no idea how to free it. :

    Code:
    LLIST *ll_create()
        {
            LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
            pthread_mutex_init(&l->lock, NULL);
            return l;
        }
    3. same story with new :
    Code:
        LL_NODE *ll_prepend(LLIST *l, void *obj)
        {
            if (l && obj) {
                
                LL_NODE *new;
                if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
        
                new->obj = obj;
                ll_lock(l);
                new->nxt = l->initial;
        
                l->initial = new;
                if (!l->last)
                    l->last = l->initial;
                l->count++;
                ll_unlock(l);
        
                return new;
            }
        
            return NULL;
        }
    For more functions u could see module-datastruct-llist.c


    Would highly appreciate , if any expert tell me , how could i fix that memory leak (if u feel , cs_malloc should be rewritten , or need to add new fucntion , please write the source code u are meaning.

    Thanks in advance
    Last edited by jimycn; 10-16-2012 at 12:41 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > i had written cs_malloc (put bellow code from oscam-simple.c in above link) , mtrace log says , that in line :
    Well it's bound to.
    mtrace only identifies the caller of malloc.
    And since you wrapped malloc in cs_malloc, then ALL calls to malloc appear to come from cs_malloc.

    Unfortunately, this is pretty useless to you, since what you really want to know is who called cs_malloc for each leak.

    > 2. also "l" use in bellow function , again since it use in return function , i have no idea how to free it. :
    You mean you have no
    void ll_delete( LLIST *l );
    function?

    You call it when you're done with the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    You mean you have no
    void ll_delete( LLIST *l );
    function?

    You call it when you're done with the list.
    @Salem
    1 . could u please write ll_delete source code (u were meaning)

    2. as the link in first post of this thread says , i think, the app has some functions like ll_remove_data() , which seems clear elements , but seems they would not free (l) , could u please take a look at them , if it's need any correction (fix) tell me.


    Code:
    int32_t ll_remove(LLIST *l, void *obj)
    {
        int32_t n = 0;
        LL_ITER it = ll_iter_create(l);
        void *data;
        while ((data=ll_iter_next(&it))) {
              if (data==obj) {
                ll_iter_remove(&it);
                n++;
            }
        }
        return n;
    }
    
    void ll_remove_data(LLIST *l, void *obj)
    {
        LL_ITER it = ll_iter_create(l);
        void *data;
        while ((data=ll_iter_next(&it))) {
          if (data==obj)
            ll_iter_remove_data(&it);
        }
    }
    
    // removes all elements from l where elements are in elements_to_remove 
    int32_t ll_remove_all(LLIST *l, LLIST *elements_to_remove)
    {
            int32_t count = 0;
            LL_ITER it1 = ll_iter_create(l);
            LL_ITER it2 = ll_iter_create(elements_to_remove);
            
            void *data1, *data2;
            while ((data1=ll_iter_next(&it1))) {
                    ll_iter_reset(&it2);
                    while ((data2=ll_iter_next(&it2))) {
                            if (data1 == data2) {
                                    ll_iter_remove(&it1);
                                    count++;
                                    break;
                            }
                    }
            }
    
            return count;
    }

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,483
    1 . could u please write ll_delete source code (u were meaning)
    Do you remember the example I gave you in your last thread? Send a pointer to the allocated memory and free it in the subroutine/function
    mtrace shows memory not freed , seems malloc memory leaks
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    [nevermind. didn't see your comment about not having valgrind]
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > 1 . could u please write ll_delete source code (u were meaning)
    No.
    Besides, it seems it's already implemented as ll_clear

    Actually, you might investigate what add_garbage() does, as that is where all deleted nodes go to die.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Actually, you might investigate what add_garbage() does, as that is where all deleted nodes go to die.
    @Salem
    how could i investiage what add_garbage() does?
    as said in first post , valgrind is not supported by sh4 , so i can't use it
    but i could use gdb , mtrace, is there any other tools or method u could advice for investigating the problem?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    I dunno - I assumed it was some other function in that package you downloaded.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    guys , the good news for me is , i had found some other users post valgrind log , i had seen those logs, but since it's first time i see a valgrind log , i didn't achieve to get any fix myself.
    here is The trac browser (source codes)
    http://www.streamboard.tv/oscam/browser/trunk/?rev=5375


    would highly appreciate if any expert would take a look at these log (i really need help) , here are 4 valgrind logs :

    valgrind (2).txt - 4shared

    valgrind.log.txt

    valgrind.txt

    valgrind (1).txt

    P.S :
    my concern for fixing memory leaks , are mostly limited to these modules :
    module-datastruct-list.c
    oscam-garbage.c
    oscam.c
    module-cccam.c
    module-dvbapi.c

    Thanks in advance
    Last edited by jimycn; 10-17-2012 at 03:15 AM.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    It seems to me that you're trying to get us to do all your work for you.

    I'm not interested in getting that large package you're working on memory leak free while you get all the recognition for it.

    If you really want to contribute to it, then
    - learn some C
    - learn some debugging skills.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    It seems to me that you're trying to get us to do all your work for you.
    this is wrong believe , if i knew how could i fix that memory leak (with those valgrind log) i already had fixed it myself (and i didn't spend my time for creating thread , searching and uploading those valgrind log in 4shared and sharing source codes here)

    i have no idea (clue) to how fix memory leaks from valgrind log , i didn't ask u to fix all those memory leaks (since this is my interest (work ) i had just asked , one expert , be such a kind, and show me how he could fix valgrind memory leaks (if he would show me , a fix for even one line of those above valrgind error log , then , i would learn , and do all the rest job myself , and i really be thankful.

    And if This place is not correct place for asking c programming (and learning) , then this is my fault to be here.
    Last edited by jimycn; 10-17-2012 at 04:26 AM.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,483
    I told you how to fix memory leaks - By freeing the memory allocated. If you missed an allocation free(), Valgrind will tell you -> Keep going through until you don't have any memory leaks any more.

    You asked if it can be done in a subroutine/function -> I said yes -> I then gave you a small example showing that functionality for you to work from.

    This site has something else that you may find useful
    Cprogramming.com: Get Expert Advice - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > i have no idea (clue) to how fix memory leaks from valgrind log
    Here, practice and learn.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void foo ( ) {
        char *leaked = malloc(10);    
    }
    char *bar ( ) {
        char *overrun = malloc(5);
        strcpy(overrun,"hello");
        return overrun;    
    }
    
    int main ( ) {
        char *p = bar();
        foo();
        printf("%s\n",p);
        free(p);
        return 0;
    }
    $ gcc -g foo.c
    $ ./a.out 
    hello
    $ valgrind --leak-check=full ./a.out
    ==2996== Memcheck, a memory error detector
    ==2996== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==2996== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==2996== Command: ./a.out
    ==2996== 
    ==2996== Invalid write of size 2
    ==2996==    at 0x4005C3: bar (foo.c:10)
    ==2996==    by 0x4005DE: main (foo.c:15)
    ==2996==  Address 0x51d2044 is 4 bytes inside a block of size 5 alloc'd
    ==2996==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2996==    by 0x4005AD: bar (foo.c:9)
    ==2996==    by 0x4005DE: main (foo.c:15)
    ==2996== 
    ==2996== Invalid read of size 1
    ==2996==    at 0x4C29741: __GI_strlen (mc_replace_strmem.c:284)
    ==2996==    by 0x4E9BF6B: puts (ioputs.c:37)
    ==2996==    by 0x4005F8: main (foo.c:17)
    ==2996==  Address 0x51d2045 is 0 bytes after a block of size 5 alloc'd
    ==2996==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2996==    by 0x4005AD: bar (foo.c:9)
    ==2996==    by 0x4005DE: main (foo.c:15)
    ==2996== 
    hello
    ==2996== 
    ==2996== HEAP SUMMARY:
    ==2996==     in use at exit: 10 bytes in 1 blocks
    ==2996==   total heap usage: 2 allocs, 1 frees, 15 bytes allocated
    ==2996== 
    ==2996== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==2996==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2996==    by 0x400595: foo (foo.c:6)
    ==2996==    by 0x4005EC: main (foo.c:16)
    ==2996== 
    ==2996== LEAK SUMMARY:
    ==2996==    definitely lost: 10 bytes in 1 blocks
    ==2996==    indirectly lost: 0 bytes in 0 blocks
    ==2996==      possibly lost: 0 bytes in 0 blocks
    ==2996==    still reachable: 0 bytes in 0 blocks
    ==2996==         suppressed: 0 bytes in 0 blocks
    ==2996== 
    ==2996== For counts of detected and suppressed errors, rerun with: -v
    ==2996== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 4 from 4)
    Can you, by inspecting the code, figure out where the problems are?
    Can you then relate the problems you know exist, see those same problems in the valgrind log?
    whiteflags likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about C malloc and free
    By lkiversonlk in forum C Programming
    Replies: 4
    Last Post: 03-25-2012, 04:52 AM
  2. malloc/free of return value
    By Bladactania in forum C Programming
    Replies: 15
    Last Post: 02-10-2009, 12:04 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. Malloc/Free
    By JupiterV2 in forum C Programming
    Replies: 5
    Last Post: 09-08-2006, 11:26 PM
  5. Malloc and Free.....
    By heljy in forum C Programming
    Replies: 5
    Last Post: 04-14-2002, 09:17 PM

Tags for this Thread


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