2015-11-30 49 views
1

當我有這個函數寫一些日誌信息保存到文件未處理的異常,有時叫fcout

int Comm::saveInfo(LPTSTR msg) 
{ 
#ifdef _log 
    wofstream fcout(LogFile, ios::out | ios::app); 
    if (fcout.is_open()) 
    { 

     fcout << msg << "\t" << (int) ::GetTickCount64() << endl; 
     fcout.close(); 
    } 
#endif 
    return 0; // this is where visual studio 2012 is pointing to for the crash 
} 

此功能,但有時有時會崩潰... :(

這是我如何把這個功能:

saveInfo(TEXT("set measurement time...")); 
ret = readMsg(Buf, WAITTIMOUT); 

Comm類的頭文件我聲明std::wstring LogFile;作爲私有成員readMsg是讀取PIP功能Ë

int CpipeComm::readMsg(LPTSTR chBuf, DWORD timeout) 
{ 

    OVERLAPPED overlapped; 
    memset(&overlapped, 0, sizeof(overlapped)); 
    memset(&chBuf[0], 0, sizeof(chBuf)); 

    int err = 0; 
    ULONGLONG time1 = ::GetTickCount64(); 
    DWORD bytesRead = 0; 
    do 
    { 
    // Read from the pipe. 
     fSuccess = ReadFile( 
     hPipe, // pipe handle 
     chBuf, // buffer to receive reply 
     BUFSIZE*sizeof(TCHAR), // size of buffer 
     &cbRead, // number of bytes read 
     &overlapped); // not overlapped 

return 0; 
} 

而且在通訊類的構造函數我做

LogFile.assign(L"Pipe_log.log"); 

,我得到的是隨機誤差;

Unhandled exception at 0x000007FEF782AFD3 (msvcp110.dll) in : 0xC0000005: Access violation reading location 0x0000000000000012. 

我不確定是否有什麼我做錯了!不知何故,這兩個功能一個接一個地導致崩潰!如果我將saveInfo函數移到了另一個地方,它的工作很好...

+0

看起來你可能會被提領一空指針。不幸的是,這裏沒有足夠的真正診斷問題。 – Cornstalks

+0

聽起來就像是在訪問超出界限的內存......在調試器中運行你的程序,直到發生錯誤;調試器應該捕捉它並報告發生的位置。 –

回答

1

沒有有效的文件流構造函數,它以std::wstring作爲文件名。看起來這是LogFile的類型,根據您的示例進行初始化。

而應該建立這樣的流:

wofstream fcout(LogFile.c_str(), ios::out | ios::app);