升压ASIO async_write"矢量迭代器不dereferencable" [英] Boost ASIO async_write "Vector iterator not dereferencable"

查看:116
本文介绍了升压ASIO async_write"矢量迭代器不dereferencable"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在异步提升服务器程序,到目前为止,我已经得到了它连接。不过,我现在得到一个向迭代器不dereferencable的错误。

I've been working on an async boost server program, and so far I've got it to connect. However I'm now getting a "Vector iterator not dereferencable" error.

我怀疑矢量被销毁或解除引用之前,他包被发送从而导致错误。

I suspect the vector gets destroyed or dereferenced before he packet gets sent thus causing the error.

void start()
{
    Packet packet;
    packet.setOpcode(SMSG_PING);
    send(packet);
}

void send(Packet packet)
{
    cout << "DEBUG> Transferring packet with opcode " << packet.GetOpcode() << endl;
    async_write(m_socket, buffer(packet.write()), boost::bind(&Session::writeHandler, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
}

void writeHandler(const boost::system::error_code& errorCode, size_t bytesTransferred)
{
    cout << "DEBUG> Transfered " << bytesTransferred << " bytes to " << m_socket.remote_endpoint().address().to_string() << endl;
}

一旦连接后开始被调用。
packet.write()返回一个uint8_t有载体

Start gets called once a connection is made. packet.write() returns a uint8_t vector

难道问题,如果我会改变

Would it matter if I'd change

void send(Packet packet)

void send(Packet& packet)

在不涉及这个问题,但性能明智的。

Not in relation to this problem but performance wise.

推荐答案

我已经找到了解决方案,为载体将被摧毁,我做了包含所生成的包队列,他们得到处理一个接一个,现在没有被解除引用所以问题就解决了​​。

I have found a solution, as the vector would get destroyed I made a queue that contains the resulting packets and they get processed one by one, now nothing gets dereferenced so the problem is solved.

可能要改变我的队列来保存数据包类,而不是结果,但是这只是一个细节。

might want to change my queue to hold the packet class instead of the result but that's just a detail.

这篇关于升压ASIO async_write&QUOT;矢量迭代器不dereferencable&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆