2015-08-23 21 views
0

我使用「命名管道」進行(單向)服務器 - 客戶端通信。它工作到目前爲止很好,但如果我關閉客戶端程序(通過按特定按鈕),我得到錯誤:「運行時檢查失敗#2 - 圍繞變量'緩衝區'堆棧已損壞。C++運行時檢查失敗#2 - 變量「緩衝區」周圍的堆棧已損壞

我知道從命名管道緩衝區數組分配的內存,但經過長時間搜索谷歌我沒有找到一種方法來解決這個問題(去釋放它)。我不得不說,我是初學者在C + +。

代碼:

char buffer[1]; 
DWORD numBytesRead = 0; 
BOOL result = ReadFile(
     pipe, 
     buffer, // the data from the pipe will be put here 
     sizeof(buffer), // number of bytes allocated 
     &numBytesRead, // this will store number of bytes actually read 
     NULL // not using overlapped IO 
     ); 
// object recognized, write data from inertaCube into file 
      if (buffer[0] != '0'){ // do something } 

// close program by pressing left CTRL-button 
      if (GetAsyncKeyState(VK_LCONTROL)){ 
       myMeasurement.close(); 
       CloseHandle(pipe); 
       return 0; 
      } 
+0

發佈的代碼沒有問題。問題出在你沒有發佈的代碼中。 – john

+0

'來自命名管道的緩衝區數組分配內存'沒有多大意義。你想說什麼? – john

回答

0

sizeof(buffer)會給你的char大小在你的代碼,這是不t // number of bytes allocated

+0

在'char buffer [1]'中似乎分配了一個char。 –

+0

確實。剛剛發生的情況是緩衝區的大小是1(等於sizeof(char))。 – Griffin

相關問題