Thread: when calloc/malloc are vital , when could be omited.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    when calloc/malloc are vital , when could be omited.

    in codes bellow , since allocated some memory for d (and never was free) , i want to add free (d) inorder to fix usages of resources in that application. , i had tried servral parts of the code , to free it, but the result was , Segment-fault or glibc detected invalid free or undesired result (like black screen).

    the author of the codes , commented (FIXME) and said , this memory should be free after (read_from_pipe) , i already had tried it , but the result is segfault in other part of the code (which seems adding free prevent them to access this data so segfault


    Code:
    int32_t write_to_pipe(int32_t fd, int32_t id, uchar *data, int32_t n)
    {
        if (!fd) {
            cs_log("write_to_pipe: fd==0 id: %d", id);
            return -1;
        }
    
        //cs_debug_mask(D_TRACE, "write to pipe %d (%s) thread: %8X to %8X", fd, PIP_ID_TXT[id], pthread_self(), get_thread_by_pipefd(fd)->thread);
    
        uchar buf[3+sizeof(void*)];
    
        // fixme
        // copy data to allocated memory
        // needed for compatibility
        // need to be freed after read_from_pipe
    
        void *d;
        if(!cs_malloc(&d, n, -1)) return -1;
        memcpy(d, data, n);
    
        if ((id<0) || (id>PIP_ID_MAX))
            return(PIP_ID_ERR);
    
        memcpy(buf, PIP_ID_TXT[id], 3);
        memcpy(buf+3, &d, sizeof(void*));
    
        n=3+sizeof(void*);
    
        return(write(fd, buf, n));
    }
    above is the part of the code (write_to_pipe) : (if need to entire source codes , here is the link :

    oscam.c in trunk



    My question is :

    1. surely need to free d (which already allocated by cs_malloc) , author of the source in his FIXME comment's believe it should be somewhere after read_pipe , i had tried , got segfault. does any expert could guide me in which function (line) i should free this allocated memory which also not break application from working?


    2. @expert , I had just thought maybe it's not required to allocated memory for d , (i mean maybe i should omit allocation , the author in FIXme comment said need for compabality , but i am not sure what he means ) and just memcpy data to buf . am i in correct point? (actually i had tried to remove that cs_malloc , but the result was segfault in other part of code...

    Thanks in advance
    Last edited by jimycn; 02-20-2013 at 05:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using malloc instead of calloc
    By sangamesh in forum C Programming
    Replies: 6
    Last Post: 04-24-2012, 04:01 AM
  2. malloc and calloc
    By rrc55 in forum C Programming
    Replies: 15
    Last Post: 09-02-2009, 07:35 PM
  3. Malloc And Calloc
    By pradeepc in forum C Programming
    Replies: 1
    Last Post: 07-28-2007, 12:48 AM
  4. Malloc vs. Calloc
    By FCF in forum C Programming
    Replies: 13
    Last Post: 06-30-2002, 06:41 PM
  5. calloc vs malloc
    By Jubba in forum C Programming
    Replies: 2
    Last Post: 02-21-2002, 04:54 PM

Tags for this Thread