繁体   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