I am running two separate threads in C, both doing some operations. Both threads include infinite loop. When I run this program couple of times, I always get a memory leak error.
*** glibc detected *** ./a.out: free(): invalid next size (normal): 0x08652510 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x3d1501]
...
I believe it is a memory error, the problem is, when I always need to stop that program(cause im still testing it), i just terminate the program with Ctrl+C, so I believe I always miss the free(anything) command, which then causes the error.
Can you tell me how to avoid this situation? So I can free() memory even in case I terminate the program?
Next thing which comes to my mind is, when i wait a couple of minutes and then run the program again, it runs perfectly again
Thanks for any hints
void *lineOne(void *dataO)
{
struct IPlist *IPlist = dataO;
static struct ARP_entryI ARP_tableI[ARP_TABLE_SIZE];
int neigh=0; //pocet susedov
int neigh1=0; //stav pred tym
int i;
getAddress();
while(1)
{
while (neigh == neigh1)
{
neigh =rcvBroad(IPlist, neigh);
}
neigh1=neigh;
for (i=neigh ; i<neigh+1; i++)
{
main_client(ARP_tableI, IPlist[i-1].IPaddr); // vysle tcp, prijme arp
getAddress();
}
}
}
//pocuvaServer, odpoveda ARP
void *lineTwo()
{
static struct ARP_entryO ARP_tableO[ARP_TABLE_SIZE];
int line = from_local_arp(ARP_tableO);
main_server(ARP_tableO, line); // pocuva tcp, vysle arp
}
void main()
{
static struct IPlist *IPlist[ARP_TABLE_SIZE];
pthread_t thread1, thread2;
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, lineOne, (void *)IPlist); //(void *) &
iret2 = pthread_create( &thread2, NULL, lineTwo, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
you could handle SIGINT, but it doesn’t matter, your code already corrupts the memory by the time you want to do that extra
free()call.to find the problem compile it with
-gand run it withvalgrind.