Hi all;

        I am having problem with a C application I have written in which the application stops executing unexpectedly.  The application makes heavy use of winsock.h and has an optional thread that can be started using "_beginthread" from process.h.  I have try updating to the latest version of MinGW, but have had no luck.  The app is about 1300 lines of code, so I don’t think size is an issue.  Also that application has been compiled on MS VS 6 with out problem.  The version from VS works fine.  So here's what I know:

When running in Single-thread mode (ie the 2 thread has not been started), the application fails during a call to malloc.  During execution, I can see the application reach the malloc call, but it never returns.  The application will stall there for about 15 seconds, and then return the shell prompt.  The application uses just over 2 MB on a Windows XP SP2 computer that has 2 GB.

The weirdest part about the problem is that it works perfectly when running inside of GDB.  The other time the application stops execution is during the start-up of the second thread.  The application completes the "_beginthread" call successfully, returns to the user input prompt and dies.  The function for the thread is listed below, but I do not believe the thread has even started, as the very first line in the thread's function is a printf that does not get printed. 

This is an application that has been running for about 5 month and has grown is functionality.  I've spent about 9 hours looking for a solution, but haven’t found one yet.  Any ideas would be greatly appreciated.  Thanks!

Compile command: "gcc -o appName appName.c -mthreads -lwsock32"
GCC Version information: "gcc (GCC) 3.4.2 (mingw-special)"
Applciation Type: Network client using TCP, Console only.
Version Information:
        01/17/2006  02:12 PM            54,478 libwsock32.a
        01/20/2005  09:59 AM         4,419,584 cc1.exe
        01/20/2005  09:59 AM         4,483,072 cc1obj.exe
        01/20/2005  09:59 AM         4,893,696 cc1plus.exe
        01/20/2005  09:59 AM            87,552 collect2.exe    
        11/18/2006  05:54 PM            11,643 mingwm10.dll


Sample code:
----------------
        case 's': case 'S':
            printf("Selection: send\n");
            if (msg == 0 && jam == 0)
            {
               fprintf(stderr,"ERROR: No message to send!");
               break;
            }
            else if ( jam != 0)
            {
               printf("\t\tcalling send buffer...");
               SendBuffer(sendBuffer, sendBufferLen);
               printf("returned from SendBuffer\n");
            }
            else
            {
               bzero(sendBuffer,sendBufferLen);
               sendBufferLen = SendOTPMessage(sendBuffer, msg);
            }
            printf("Send: status of IsAutoReceiveActive: %d\n",IsAutoReceiveActive);
            if (IsAutoReceiveActive) break;
         case 'r': case 'R':
            printf("\t\tEntering Recv phase...");
            if ( rcvmsg != 0)
            {
               printf("clearing rcvsg buffer..");
               if (rcvmsg->raw_message != NULL)
                 free(rcvmsg->raw_message);
               free(rcvmsg);
               rcvmsg = 0;
               printf("mem cleared\n");
            }
            printf("Zeroing buffer...");
            bzero(rcvBuffer,MAX_BUFFER);
            printf("done.\n Starting malloc... sizeof: %d", sizeof(OTP_MESSAGE));
            rcvmsg = malloc(16); /* <------------------------------- APPLICATION EXECUTION DIES HERE */
            printf("done.\n Checking malloc result...");
            if ( rcvmsg == NULL )
            {
               printf("WARNING!  WAS NULL!!\n");
               fprintf(stderr,"ERROR Malloc failed during creation of rcvmsg\n");
               exit(-121);
            }
------
End Section.

Sample Code for Thread:
---------
void AutoReceive(void* param)
{
   fprintf(stdout,"started....\n");
   uchar buffer[MAX_BUFFER] = {0};
   int buffer_size = 0;
   char* name;
   int name_len;
   int result = 0;
   DWORD dwWaitResult;
   time_t secs;

   IsAutoReceiveActive = 1;
   fprintf(stdout, "\t\tinside thread, calling malloc...");
   fflush(stdout);
   name = (char*) malloc(MAX_NAME_SIZE + 1);
   fprintf(stdout,"done.\n");

   while (!IsOkToExit && IsConnected && !ExitAutoReceive)
   {
      buffer_size = ReceiveBuffer(buffer, 1);
      if ( buffer_size > 0 )
      {
         printf("got %d bytes from server\n",buffer_size);
         time(&secs);
         strncpy(name,ctime(&secs),MAX_NAME_SIZE);
         printf("requesting mutex....");
         if( WaitForSingleObject(hRecMutex, MUTEX_TO) == WAIT_OBJECT_0)
         {
            printf("got mutex\n");
            result = SaveBuffer(buffer, buffer_size, name, recvMsgs, &recvMsgs_size, 1);
            printf("completed save with result=%d\n",result);
            ReleaseMutex(hRecMutex);
            printf("Released mutex\n");
            if ( result == -2)
            {
               fprintf(stderr,"ERROR: Receive buffer full, ending Auto-Receive.\n");
               ExitAutoReceive = 1;
            }
            name_len = strlen(name);
            if ( name_len > MAX_NAME_SIZE )
               name_len = MAX_NAME_SIZE;
            bzero(name,name_len);
         }
         else
         {
            printf("didn't get mutex\n");
            ExitAutoReceive = 1;
            fprintf(stderr,"\nERROR: Unable to obtain ReceiveMutex.\n");
         }
      }
      else
         printf("timed-out...\n");
   }
   printf("freeing name...\n");
   free(name);
   IsAutoReceiveActive = 0;
}
---------


Dan Hanson