我在以下代码中遇到了段错误,但我无法弄清楚为什么...... 我正在尝试使用一个最小的异步本地套接字服务器而不使用类似于在示例中完成的类来查看更好的调用。我一直小心不要忘记任何事情,但是我一直在搜索2小时而没有找到为什么我有这个Segfault ......
这是我的代码:
#include <iostream>
#include <asio.hpp>
const char * const path = "/var/local/serv.socket";
asio::local::stream_protocol::acceptor * acceptor;
asio::io_service io_service;
void handle_co(std::error_code ec){
std::cout << ec.message() << std::endl;
std::cout << "Connection !";
}
void loop(){
auto socket = new asio::local::stream_protocol::socket(io_service);
acceptor->async_accept(*socket, [socket](std::error_code ec){
handle_co(ec);
socket->close();
delete socket;
loop();
});
}
int main(int argc, char **argv) {
std::remove(path);
asio::local::stream_protocol::endpoint endpoint(path);
asio::local::stream_protocol::acceptor acceptor(io_service);
acceptor.open(endpoint.protocol());
acceptor.bind(endpoint);
acceptor.listen();
loop();
io_service.run();
return 0;
}
...即使您只需编译并运行此代码,这也是valgrind给我的:
==20787== Invalid read of size 8
==20787== at 0x418E14: asio::basic_io_object<asio::socket_acceptor_service<asio::local::stream_protocol>, true>::get_service() (basic_io_object.hpp:208)
==20787== by 0x411909: asio::async_result<asio::handler_type<loop()::{lambda(std::error_code)#1}, void (std::error_code)>::type>::type asio::basic_socket_acceptor<asio::local::stream_protocol, asio::socket_acceptor_service<asio::local::stream_protocol> >::async_accept<asio::local::stream_protocol, asio::stream_socket_service<asio::local::stream_protocol>, loop()::{lambda(std::error_code)#1}>(asio::basic_socket<asio::local::stream_protocol, asio::stream_socket_service<asio::local::stream_protocol> >&, asio::handler_type&&, std::enable_if<std::is_convertible<asio::local::stream_protocol, asio::basic_socket>::value, void>::type*) (basic_socket_acceptor.hpp:1018)
==20787== by 0x4117BE: loop() (main.cpp:23)
==20787== by 0x41188C: main (main.cpp:33)
==20787== Address 0x18 is not stack'd, malloc'd or (recently) free'd
==20787==
==20787==
==20787== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==20787== Access not within mapped region at address 0x18
==20787== at 0x418E14: asio::basic_io_object<asio::socket_acceptor_service<asio::local::stream_protocol>, true>::get_service() (basic_io_object.hpp:208)
==20787== by 0x411909: asio::async_result<asio::handler_type<loop()::{lambda(std::error_code)#1}, void (std::error_code)>::type>::type asio::basic_socket_acceptor<asio::local::stream_protocol, asio::socket_acceptor_service<asio::local::stream_protocol> >::async_accept<asio::local::stream_protocol, asio::stream_socket_service<asio::local::stream_protocol>, loop()::{lambda(std::error_code)#1}>(asio::basic_socket<asio::local::stream_protocol, asio::stream_socket_service<asio::local::stream_protocol> >&, asio::handler_type&&, std::enable_if<std::is_convertible<asio::local::stream_protocol, asio::basic_socket>::value, void>::type*) (basic_socket_acceptor.hpp:1018)
==20787== by 0x4117BE: loop() (main.cpp:23)
==20787== by 0x41188C: main (main.cpp:33)
答案 0 :(得分:0)
问题是因为您有范围名称冲突。在全球范围内,您声明acceptor * acceptor
,然后在acceptor
中声明main()
并初始化它。
稍后,在loop()
函数内部使用未初始化的acceptor*
并且崩溃。像这样替换 aceptor 初始化及其工作原理:
acceptor = new asio::local::stream_protocol::acceptor(io_service);
acceptor->open(endpoint.protocol());
acceptor->bind(endpoint);
acceptor->listen();