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