Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows Win32 Visual-Studio , +
My following code gives an error "Run-Time Check Failure # 2 - Stack around variable 'thread no' was corrupted." if I add a "break" inside my if statement. I have also hghlighted this "break" inside the code. If I remove thi "break", the error is removed.
 
Can anyone explain me please why does this happen? Frown | :(

P.S: Sorry for the long code.

 
Code:
 
DWORD WINAPI ThreadProc(LPVOID param)
    {
       int threadNumber= (int)param;
       int PORT = 8888+threadNumber; //so that each thread bind()s its socket to a different Port number.
       WSADATA wsa; 
 
       //Initialise winsock//
       if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
          {
 
            //"WinSock Initialization FAILED",

          }
 
       //Create a socket//

      SOCKET newSocketIdentifier;
      SOCKADDR_IN newSocket;
 
      if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
          {                 
 
            //Socket Creation Failed

          }
       //Socket Created//

       //Prepare the sockaddr_in structure//
      newSocket.sin_family = AF_INET;
      newSocket.sin_addr.s_addr = INADDR_ANY;
      newSocket.sin_port = htons(PORT);
 
       //Bind//
       if( bind(newSocketIdentifier ,(struct sockaddr *)&newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
          { 
           //Bind Failed
          }
 
       //Bind Done//   char threadNumberBuffer[32] = "Thread Number : ";
       char buff[12];
       itoa(threadNumber,buff,10);
       strcat(threadNumberBuffer,buff);
       MessageBox( NULL,
                   threadNumberBuffer,
                   "Thread Created :)",
                   MB_ICONINFORMATION);
 

    char data[256];
    int bytes, waitRet;
 
    WSAEVENT hEvent = WSACreateEvent();
    WSANETWORKEVENTS events;
    WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE);
 
    SOCKADDR_IN clientSocket;
    int clientSocketLength = sizeof(SOCKADDR_IN);
    char receiveBuffer[8192]={0};
    int recv_len=0;
    char file[12]="Client";
    char threadno[2];
    itoa(threadNumber,threadno,10);
    strcat(threadno,".txt");
    strcat(file,threadno);
    //FILE *fprecv;
    FILE *fprecv = fopen(TEXT(file),"w+b");
    //rewind(fprecv);
    int receiveCount=0;
 
       while(1)
          {   //while(1) starts
             waitRet = WSAWaitForMultipleEvents(1, &hEvent, FALSE, WSA_INFINITE, FALSE);
             //WSAResetEvent(hEvent);
             if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&events) == SOCKET_ERROR)
                {
                   MessageBox( NULL,
                               threadNumberBuffer,
                               "FAILURE",
                               MB_ICONINFORMATION);
                }
            else
                {   //else event occurred starts
                   if(events.lNetworkEvents & FD_READ)
                      {   //check for network event starts
                         /*MessageBox( NULL,
                                     buf,
                                     "FD_READ",
                                     MB_ICONINFORMATION);*/
 
                         if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
                            {
                               MessageBox( NULL,
                                           "ERROR",
                                           "Could not Receive Data",
                                           MB_ICONINFORMATION);
                               exit(EXIT_FAILURE);
                               return FALSE;
                            }
                         receiveCount = receiveCount+1;
                         char display[2000] = "Number of Receives = ";
 
                        if(memcmp(receiveBuffer,"EXIT",4) == 0)
                            {
                                char receiveCountBuffer[128];
                                itoa(receiveCount,receiveCountBuffer,10);
                                strcat(display,receiveCountBuffer);
                                MessageBox( NULL,
                                            display,
                                            threadNumberBuffer,
                                            MB_ICONINFORMATION);
                                break; //Main Problem is here. if I remove this break statement, the error is removed. Else I get Run-Time Check Failure # 2.
                            }
                    /*MessageBox( NULL,
                                receiveBuffer,
                                "File Read",
                                MB_ICONINFORMATION);*/
                        else
                        {
                              fprecv= freopen(TEXT(file),"w+b",stdout);
                               if(fwrite(receiveBuffer, 1, recv_len, fprecv)<0)
                                  {
                                     MessageBox( NULL,
                                                 "problem while writing file",
                                                 "Error!",
                                                 MB_ICONINFORMATION);
 
                                     exit(1);
                                  }
                               fclose(fprecv);
                        }
 
                               //rewind(fprecv);
                   }   //check for network event ends
                }   //else event occurred ends
          }   //while(1) ends

        WSACloseEvent(hEvent);
        return 0;
    }
Posted 8-May-13 2:32am
Edited 8-May-13 2:36am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The error message indicates stack problems around threadno. And it is really helpful here when looking at the occurences for that variable:
char threadno[2];
itoa(threadNumber,threadno,10);
strcat(threadno,".txt");
Did you see the problem? The string can hold only one character plus NULL byte. But you are copying an integer as string (which will work if the value is smaller than 10) and appending a 4 character wide string which overruns the buffer.
  Permalink  
Comments
ayesha hassan at 8-May-13 8:05am
   
OMG. When I was posting this question, I was dead sure that nobody is going to go through a code this long. I am shocked to see that people here are so helping that they spare their time to find out the stupid errors made by people like me :) I am really thankful for the help :)
Jochen Arndt at 8-May-13 8:09am
   
You are welcome.
But I did not had to go through all the code but searched for the occurence of the variable name and saw the problem immediately.
ayesha hassan at 9-May-13 0:48am
   
I got the idea now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100