Reputation: 11
void *insert_rear_node(void *arg)
{
int *argument=(int *)arg;
int value=*argument;
//Assume that allocation is happening fine (Just like malloc , it is a custom heap allocator)
struct Node *head=(struct Node *) RTallocate(RTpointers, sizeof(struct Node));
struct Node *temp;
setf_init(temp,head);
while(value>0)
{
if(temp==NULL)
{
RTwrite_barrier(&(temp), new_node(value,NULL));
RTwrite_barrier(&(head),temp);
printf("Inserted %d into the list\n",head->data);
}
else
{
RTwrite_barrier(&(temp),head);
while(temp->next!=NULL)
{
RTwrite_barrier(&(temp),temp->next);
}
RTwrite_barrier(&(temp->next),new_node(value,NULL));
printf("Inserted %d into the list\n",temp->next->data);
}
value=value-1;
}
free(head);
}
int main(int argc, char *argv[])
{
long heap_size = (1L << 28);
long static_size = (1L << 26);
printf("heap_size is %ld\n", heap_size);
RTinit_heap(heap_size, static_size, 0);
pthread_t thread1;
int limit=1000;
RTpthread_create(&thread1,NULL,insert_rear_node,(void *)&limit);
}
Assume that RTallocate and RTwrite_barrier are 2 custom functions which work perfectly fine. RTallocate - allocates memory on the heap RTwrite_barrier is equivalent to an assignemnt statement.
This program simply insert nodes into a linkedlist. Then attempts to delete the head.
I get this error:
Error in `/home/test/RT-Test/InMul': double free or corruption (out): 0x00007fffe3465010
warning: Corrupted shared library list: 0x7ffea4000920 != 0x7ffff7ffd9d8
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff77f97e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7ffff7801e0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff780598c]
/home/test/RT-Test/InMul[0x400b98]
/lib/x86_64-linux-gnu/librtgc.so(rtalloc_start_thread+0x1ef)[0x7ffff7b4fa2c] /lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7ffff756c6ba] /lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7ffff788882d]
I am freeing the head only once. Why am I facing this issue?
0x00007fffe3465010 is the address of the head.
Upvotes: 1
Views: 2049
Reputation: 140569
Some piece of code you didn't show us, quite probably nowhere near the code you think is the problem, has corrupted malloc
's internal bookkeeping data.
Run your program under valgrind
and fix the very first invalid operation that it reports. Repeat until valgrind has no more complaints. Now your program should work. If you do not understand valgrind's output, edit the first 20 or so lines of it into your question and we may be able to explain.
Upvotes: 1
Reputation: 651
You can't use free() when you didn't use malloc() for head. Use the free equivalent to RTallocate.
Upvotes: 1