简体   繁体   中英

Memory error with free()

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 -g and run it with valgrind .

use a signal handler to catch the Ctrl-C event (Ctrl-C generates a SIGINT signal), and set a flag in the handler. modify the infinite loop so that it stop looping when it sees the flag, and write a cleanup code after the loop. your program will then end "normally".

the signal handling functions are part of the GNU C library (and of any POSIX system, i think). here is a link to the documentation of the gnu c library regarding signal handling.

Try running your program through Valgrind and see if you can get any help on where the memory allocation structure is corrupted. From the error it looks like you are somewhere doing something invalid with the memory allocation the corrupts the internal data structures for memory allocation.

You have corrupted your heap space. Perhaps you are writing off the end (or before the beginning) of a chunk of allocated memory. The free is detecting the corruption and producing the error.

When the program is terminated all memory will be freed automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM