2013-02-02 60 views
1

我正在嘗試爲tcpip設置異步I/O的升壓測試。升壓測試和異步IO:在地址的內存訪問衝突:在故障地址沒有映射

我的函數來發送TCP消息:

int TcpClient::sendMsgToServer(string msg) { 
    if (isConnected == true) { 
     Logger::debug("Asynch send request for msg: " + msg, __LINE__, 
       __FILE__); 
     sendSequence++; 
     msg.append("\r\n"); 
     int length = msg.length() + 2; //+2 is for \r\n 
     char* buffer = (char*) msg.c_str(); 
     Logger::debug(buffer, __LINE__, __FILE__); 
     m_Socket.async_send(boost::asio::buffer(buffer, length), 
       boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler, 
         this, boost::asio::placeholders::error, 
         boost::asio::placeholders::bytes_transferred)); 
     std::string sentMsg = "Sent Msg: " + msg; 
     Logger::debug(sentMsg, __LINE__, __FILE__); 


     return 0; 
    } else { 
     return -1; 
    } 

} 

    void TcpClient::sendMsgToServerErrorHandler(
      const boost::system::error_code& error, std::size_t bytes_transferred) { 
     Logger::debug("Sending complete: ", __LINE__, __FILE__); 
     sendResponseSequence++; 
     int i = error.value(); 
     std::stringstream ss; 
     ss.clear(); 
     ss << i; 
     Logger::debug("RESULTCODE: " + ss.str(), __LINE__, __FILE__); 
     if (i == 0) { 
      Logger::debug("RESULT: " + error.message(), __LINE__, __FILE__); 

     } else { 
      Logger::crit(error.message(), __LINE__, __FILE__); 
      m_Socket.close(); 
      isConnected = false; 
      connectionStateChanged(isConnected); 
     } 

    } 

現在此功能工作正常,如果我只是在主線程中運行(不運行做升壓試驗)

我升壓測試功能看起來像這樣的:

BOOST_AUTO_TEST_CASE(CommunicationCore_sendTcpIpMsg_test) { 

    CommunicationCoreFixture f; 
    int compare = f.c->initializeTcpIpConnection(serverAddress, serverPort); // i initialize the connection here. runs fine with any issue 
    sleep(2); 
    compare = f.c->sendMsgToServer("IDENTIFY console-2"); 
    BOOST_MESSAGE("Sending returned value : " << compare); 
    BOOST_CHECK(compare == 0); 
} 

和失敗,出現以下錯誤:

Entering test case "CommunicationCore_sendTcpIpMsg_test" 
unknown location(0): fatal error in "CommunicationCore_sendTcpIpMsg_test": memory access violation at address: 0x01003255: no mapping at fault address 
*****************Test is aborted 

有什麼我應該知道,而測試這樣的異步功能?

我的生成信息:

Platform: linux 
Compiler: GNU C++ version 4.7.2 
STL  : GNU libstdc++ version 20120920 
Boost : 1.49.0 

編輯:

我曾嘗試通過以下方式修改它well..but仍然得到同樣的錯誤:

int TcpClient::sendMsgToServer(string msg) { 
    if (isConnected == true) { 
     Logger::debug("Asynch send request for msg: " + msg, __LINE__, 
       __FILE__); 
     sendSequence++; 
     msg.append("\r\n"); 
    char *buffer = new char[msg.size() + 1]; 
    buffer[msg.size()] = 0; 
    memcpy(buffer, msg.c_str(), msg.size()); 
    Logger::debug(buffer, __LINE__, __FILE__); 

    m_Socket.async_send(boost::asio::buffer(buffer, (msg.size() + 1)), 
      boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler, 
        this, boost::asio::placeholders::error, 
        boost::asio::placeholders::bytes_transferred)); 
     std::string sentMsg = "Sent Msg: " + msg; 
     Logger::debug(sentMsg, __LINE__, __FILE__); 


     return 0; 
    } else { 
     return -1; 
    } 

} 
+0

'CommunicationCoreFixture'的默認構造函數是否確保其'c'成員指向一個有效的對象? –

+0

是的。事實上,我應該提到,如果我刪除零件: m_Socket.async_send(boost :: asio :: buffer(buffer,length), boost :: bind(&fingigcc :: TcpClient :: sendMsgToServerErrorHandler, this,boost :: asio :: placeholders :: error, boost :: asio :: placeholders :: bytes_transferred));它運行沒有問題。 –

回答

3

m_Socket.async_send(boost::asio::buffer(buffer, length) - 但是msg對象是本地的,它的c_str() res在msg超出範圍後,ult將無效!所以你實際上將一個臨時緩衝區傳遞給一個異步操作。緩衝區不會超過這個操作,所以segfault是不可避免的。

+0

編輯您的建議..不確定我做對了。但仍然得到相同的錯誤 –

+0

@Shrouk哈桑汗現在看起來是正確的。如果你仍然崩潰,也許它來自另一個(但相似)的地方。此外,請檢查「TcpClient」實例是否超出了異步操作。 –

+1

事實上,你是正確的。該錯誤來自於初始化函數中的boost :: thread(這裏沒有提到),該函數沒有足夠長的時間傳遞給sendMsgToServer。 –

相關問題