簡體   English   中英

ASIO寫操作拋出std :: bad_alloc:C ++

[英]ASIO write operation throws std::bad_alloc : C++

我指的是聊天客戶端

我的寫操作是:

void CSession::beginWrite(const Buffer & message)
{
    //Check if the socket is open or not?
    bool writeInProgress = !writeQueue_.empty();
    writeQueue_.push_back(message);
    if (!writeInProgress) //Exception Thrown here
    {
        asio::async_write(socket_, asio::buffer(writeQueue_.front().received_, writeQueue_.front().buffsize),
            std::bind(&CSession::handle_write, this,
            std::placeholders::_1, std::placeholders::_2));
    }
}

void CSession::handle_write(const asio::error_code& error /*error*/, size_t bytes_transferred /*bytes_transferred*/)
{
    //std::cout << "CSession::handle_write() Called" << "(" << __FILE__ << " : " << __LINE__ << ")" << std::endl;
    if (!error)
    {
        //std::cout << bytes_transferred << " bytes written to the socket." << std::endl;
        writeQueue_.pop_front();
        if (!writeQueue_.empty())
        {
            asio::async_write(socket_, asio::buffer(writeQueue_.front().received_, writeQueue_.front().buffsize),
                std::bind(&CSession::handle_write, this,
                std::placeholders::_1, std::placeholders::_2));
        }
    }
    else
    {
        std::cout << "Write Error Detected" << std::endl;
        std::cout << error.message() << std::endl;
        state_ = false;
        doClose();
        return;
    }
}

它工作正常。 然后我嘗試通過讓客戶Client 2連續11分鍾向服務器寫入消息Client 2來加載測試,如下所示:

bool flag = false;

void setFlag(const asio::error_code& /*e*/)
{
    flag = true;
}

void Client(std::string IP, std::string port)
{
    CSession Session(IP, port);
    Session.initSession();

    asio::thread t(boost::bind(&asio::io_service::run, &(*CIOService::fetchIOService().getIO())));

    asio::deadline_timer timer(*CIOService::fetchIOService().getIO(), boost::posix_time::seconds(675));
    timer.async_wait(&setFlag);

    while (!flag)
    {
        Session.write("Client 2");
    }

    Session.close();
    t.join();
}



void main()
{
    Client("localhost", "8974");
    system("Pause");
}

成功寫入操作2-3分鍾后,代碼Unhandled exception at 0x75B7C42D in NetworkComponentsClient.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x026DE87C.拋出異常Unhandled exception at 0x75B7C42D in NetworkComponentsClient.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x026DE87C. 在線

if (!writeInProgress) //Exception Thrown here { asio::async_write(socket_, asio::buffer(writeQueue_.front().received_, writeQueue_.front().buffsize), std::bind(&CSession::handle_write, this, std::placeholders::_1, std::placeholders::_2)); }

調試顯示:

-       writeQueue_ { size=16777215 }   std::deque<channel::Buffer,std::allocator<channel::Buffer> >
+       [0] {received_=0x052a0ac8 "Client 2" }  channel::Buffer
+       [1] {received_=0x052a0b28 "Client 2" }  channel::Buffer
+       [2] {received_=0x052a0b88 "Client 2" }  channel::Buffer
....
....

我可以看到writeQueue_ { size=16777215 }非常大,因此std::bad_alloc

為何如此行為? 我可以看到來自deque的代碼彈出消息如下:

if (!error) { writeQueue_.pop_front(); if (!writeQueue_.empty()) { asio::async_write(socket_, asio::buffer(writeQueue_.front().received_, writeQueue_.front().buffsize), std::bind(&CSession::handle_write, this, std::placeholders::_1, std::placeholders::_2)); } }

所以write deque不應該變得那么大。

我的客戶應該運行數天,並且應該參與大量的連續數據寫入。 如何確保平滑的長寫操作?

您的消費者( CSession )遠比您的生產者( Client )慢。 您的生產者通過盡可能快地生成消息來進行拒絕服務攻擊。 這是一個很好的測試。

您的消費者應該(至少一個,最好是全部):

  • 檢測到工作正在積累並在發生此類事件時設置策略,例如“忽略新”,“刪除最舊”
  • 通過在傳入消息上設置活動過濾器來限制消耗延遲
  • 提高傳入消息處理的性能。

我的客戶應該運行數天,並且應該參與大量的連續數據寫入。 如何確保平滑的長寫操作?

那么你需要一個比在網上找到的例子更好的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM