如何在 OSX 上使用 boost asio 发送大于 256kByte 的缓冲区,在 WIN 上运行良好
我有一个简单的客户端服务器方法,在 OSX 上使用 tcp 套接字和 boost asio 发送字符缓冲区。当我使用大于 256kBytes 的缓冲区时,我收到内存访问错误。这似乎发生在服务器端。但尽管如此,缓冲区大小大于 256kBytes 的完全相同的代码在 Windows 上运行得很好!
OSX 上的 gdb 告诉我
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000001002bebb8
[Switching to process 15955 thread 0x2003]
0x0000000100012920 in connectionThread::operator() ()
我想知道是否存在我必须注意的操作系统特定的内存分配问题。 Boost Asio 文档并没有真正的帮助。
我不想分割缓冲区!我想发送整个缓冲区,其大小最大可达 921600Bytes。它已经在带有 boost asio 的 Windows 上完美运行。
消息
char data[307200];
客户端发送消息
void send( boost::asio::ip::tcp::socket& s ) const {
try {
boost::asio::write( s, boost::asio::buffer( data, data_size));
}
catch( std::exception e ) {
throw std::runtime_error("message send error | " + std::string( e.what() ) );
}
}
服务器端读取消息
void read( boost::asio::ip::tcp::socket& s ) {
try {
// Read data
boost::asio::read( s, boost::asio::buffer( &data, data_size ));
}
catch( std::exception e ) {
throw std::runtime_error( "message read error | " + std::string( e.what() ) );
}
}
回溯
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000001002bebb8
[Switching to process 17916 thread 0x2003]
0x0000000100012920 in connectionThread::operator() ()
(gdb) backtrace
#0 0x0000000100012920 in connectionThread::operator() ()
#1 0x000000010000679d in boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>::operator() ()
#2 0x00000001000068ca in boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > >::operator()<boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list0> ()
#3 0x000000010000690a in boost::_bi::bind_t<void, boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >::operator() ()
#4 0x0000000100006928 in boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >::run ()
#5 0x000000010006b699 in thread_proxy (param=<value temporarily unavailable, due to optimizations>) at libs/thread/src/pthread/thread.cpp:121
#6 0x00007fff8094cfd6 in _pthread_start ()
#7 0x00007fff8094ce89 in thread_start ()
(gdb)
感谢您的任何想法
I have a simple client server approach sending char buffers using tcp socket with boost asio on OSX. When I use a buffer bigger than 256kBytes I get a Memory Access error. It seems to happen on the Server side. But nonetheless, the exact same code with bigger buffer sizes than 256kBytes is working perfectly on Windows!
gdb on OSX tells me
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000001002bebb8
[Switching to process 15955 thread 0x2003]
0x0000000100012920 in connectionThread::operator() ()
I'm wondering if there is a OS specific memory allocation issue I have to be aware of. Boost Asio docs weren't really helpful.
I do not want to split the buffer! I want to send an entire buffer, which could have a size up to 921600Bytes. Which already works perfectly on windows with boost asio.
Message
char data[307200];
Client side SEND msg
void send( boost::asio::ip::tcp::socket& s ) const {
try {
boost::asio::write( s, boost::asio::buffer( data, data_size));
}
catch( std::exception e ) {
throw std::runtime_error("message send error | " + std::string( e.what() ) );
}
}
Server side READ msg
void read( boost::asio::ip::tcp::socket& s ) {
try {
// Read data
boost::asio::read( s, boost::asio::buffer( &data, data_size ));
}
catch( std::exception e ) {
throw std::runtime_error( "message read error | " + std::string( e.what() ) );
}
}
backtrace
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000001002bebb8
[Switching to process 17916 thread 0x2003]
0x0000000100012920 in connectionThread::operator() ()
(gdb) backtrace
#0 0x0000000100012920 in connectionThread::operator() ()
#1 0x000000010000679d in boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>::operator() ()
#2 0x00000001000068ca in boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > >::operator()<boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list0> ()
#3 0x000000010000690a in boost::_bi::bind_t<void, boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >::operator() ()
#4 0x0000000100006928 in boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, connectionThread, boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >&>, boost::_bi::list2<boost::_bi::value<connectionThread*>, boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >::run ()
#5 0x000000010006b699 in thread_proxy (param=<value temporarily unavailable, due to optimizations>) at libs/thread/src/pthread/thread.cpp:121
#6 0x00007fff8094cfd6 in _pthread_start ()
#7 0x00007fff8094ce89 in thread_start ()
(gdb)
Thanks for any ideas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论