C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-18-2003, 03:52 PM   #1
Registered User
 
Join Date: Jun 2003
Posts: 41
Unhappy SIGABRT upon free()

Hello all,

For some reason when I free(buffer) below, I always get a SIGABRT and I have no clue why.
Looking at the values buffer contains just before the free(), it has exactly what I'd expect,
just a few bytes worth of data, and num_bytes seems to have the correct amount. The original
malloc() returns a valid address, not NULL, so that seemed to work. Any ideas??


Code:
int ReceiveFeedback(comm_data_t* port, char *result, int *MPTR, int *timestamp)
{
   int       bytesReceivedSoFar = 0;  /* how many bytes we've read thus far */
   int       bufferSize = 1024;    /* size of buffer */
   int       select_value;         /* what select() returns */
   char      *buffer;              /* what we can grab from our port at each select iteration */
   char      *stringSoFar;         /* our total string built thus-far */
   int       num_bytes;            /* the number of bytes we read from our port */
   fd_set    watchset;             /* fd_set we want to watch for reading */
   struct timeval loopdelay;       /* str dictating how long select() should wait */
   int       junkInt;              /* garbage used to double-check sscanf */
   char      junkChar;             /* garbage used to double-check sscanf */
   const int bytesExpectedForSuccess = 40;  /* minimum bytes needed for success. stringSoFar value tested also */

   buffer = malloc (sizeof(char) * (2000));
   strcpy(buffer, "");

   stringSoFar = malloc (sizeof(char) * (30000));
   strcpy(stringSoFar, "");

   while (1)
   {
      strcpy(buffer, "");
      loopdelay.tv_sec = 5;  /* wait a long time before giving up */
      loopdelay.tv_usec = 0; /* must reset this for each call     */
      FD_ZERO(&watchset);    /* clear all bits in this set        */
      FD_SET(port->fd, &watchset);   /* watch our data instrument */

      /*  now keep reading data until we have our full menu */
      if ((select_value = select((port->fd)+1, &watchset, NULL, NULL, &loopdelay)) > 0)
      {
         /* all we're listening to is 1 port so no need for FD_ISSET */

         /* we got some data */
         num_bytes = read(port->fd, buffer, bufferSize);

	 if (num_bytes > 0)
	 {
	    /* we read something! */
	    /* take whatever we read this time and append it to what we've read over the past */
	    buffer[num_bytes] = 0x00;
            bytesReceivedSoFar += num_bytes;
	    strncat(stringSoFar, buffer, num_bytes);

	    /* does what we've read contain our substring? */
	    if (sscanf(stringSoFar, "%*[^A]A%i L+%i C%*c%*c%*c%c", &junkInt, MPTR, &junkChar) == 3)
	    {
	       /* we found our substring. see if there's enough data to send onward */
	       if (bytesReceivedSoFar < bytesExpectedForSuccess)
               {
	          /* we found the end of the CR10's response, but we know we didn't get any meaningful data */
                  strcpy(result, "");
		  free (buffer);
                  free (stringSoFar);
		  return (-1);
               } /* end if */

	       else
	       {
	          /* we found the end and we have enough bytes to have meaningful data */
	          strcpy(result, stringSoFar);
	          free (buffer); /* ------------------------- ALWAYS CAUSES SIGABRT on free()???? --- */
	          free (stringSoFar);
	          return (1);
	       } /* end else */

	    } /* end if sscanf == 3 */

	 } /* end if num_bytes > 0 */

      } /* end if select > 0 */

      else if (select_value == 0)
      {
         /* select returned 0, nothing more to ever read */
         strcpy(result, stringSoFar);
         free (buffer);
         free (stringSoFar);
         return (-2);
      } /* end else select == 0 */

      else
      {
         /* select returned negative value like EINTR so ignore */
	 ;
      } /* end else */

   } /* end while */

   /* we should never get here */
   strcpy(result, stringSoFar);
   free (buffer);
   free (stringSoFar);
   return (-3);

} /* end ReceiveFeedback(...) */
Thanks for any help.
registering is offline   Reply With Quote
Old 07-18-2003, 04:46 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
Code:
strcpy(buffer, "");
You should probably be memset-ting these.

You should provide debug lines and fflush the output of said lines before you call free() to see which one is crashing. (Or in fact, to be sure it is free that is actually crashing it.)

Is there a reason you need a 30K buffer?
Are you sure it is not NULL? I don't see you checking the return value of malloc any place.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 07-19-2003, 07:52 AM   #3
Registered User
 
Join Date: Jun 2003
Posts: 41
Thanks for the great feedback. I'll give your suggestions a try and see if I can improve my code.
registering is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Program that displays amount of free memory trancedeejay Linux Programming 3 01-13-2006 01:27 PM
(C, Malloc, Free) Help! Access violation and/or damage after normal block!! Raptor007 C Programming 12 06-24-2005 04:27 PM
Help needed with backtracking sjalesho C Programming 1 11-09-2003 06:28 PM
"if you love someone" :D Carlos A Brief History of Cprogramming.com 12 10-02-2003 01:10 AM


All times are GMT -6. The time now is 11:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22