运行时检查失败#2 - 围绕变量'thread no'的堆栈已损坏 [英] Run-Time Check Failure # 2 - Stack around variable 'thread no' was corrupted

查看:240
本文介绍了运行时检查失败#2 - 围绕变量'thread no'的堆栈已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的以下代码给出了错误运行时检查失败#2 - 堆栈变量''线程没有''已损坏。如果我在if语句中添加break。我还强调了代码中的这个中断。如果我删除了中断,则错误被删除。



任何人都可以解释一下,为什么会发生这种情况? :(



PS:很抱歉长码。




< b>代码:



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? :(

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;
    }

推荐答案

错误消息表示 threadno 周围的堆栈问题。在查看该变量的出现时,这非常有用:

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");



你看到了问题吗?该字符串只能包含一个字符和NULL字节。但是你将一个整数复制为字符串(如果值小于10则会起作用)并附加一个超出缓冲区的4个字符宽的字符串。


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.


这篇关于运行时检查失败#2 - 围绕变量'thread no'的堆栈已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆