求教一个C++(boost asio)服务器开发方面的问题

71 views
Skip to first unread message

nebuladream

unread,
Oct 24, 2009, 5:56:39 AM10/24/09
to 高性能服务器研发与运营邮件列表
在今天的开发当中,我们再次遇到了一个几乎无法解决的诡异问题。其问题描述如下,希望群里的大牛给予指点:
程序已经在我的机器上运行成功了,现在要移植到基于centos的64位服务器上。
我们将源代码成功编译后,运行时发生了Segmentation fault的错误提示。使用GDB进行跟踪发现错误出现在类构造的初始化列表当
中。
其中 boost::asio::ip::tcp::socket m_socket; m_socket 在进行初始化时的代码为
m_socket
(*pio),参数pio 为io_service对象的指针,我们可以保证其指针的有效性(在该语句的上一条语句中已经进行了初始化的操作)。
在跟踪进入 m_socket对象的构造函数之后,我们发现在程序运行到/root/boost_1_39_0/boost/asio/
detail/
service_registry.hpp:208 地方发生了Segmentation fault的错误提示。
我们怀疑是boost库本身的问题,于是运行了其官方提供的例子。
例子当中包括对于 boost::asio::ip::tcp::socket对象的初始化操作,但例子程序可以正常运行。

nebuladream

unread,
Oct 24, 2009, 5:57:08 AM10/24/09
to 高性能服务器研发与运营邮件列表
下面是我们使用GDB调试的代码:
Breakpoint 1, CProxy (this=0x7fffa648b4e0, filename=0x7fffa648bbe5
"config.txt") at proxy.cpp:51
51 pIConnect pcconnect(new CCconnect(&ioOwn,
ip.c_str(), port.c_str
(), this, regist,response));
(gdb) s
CCconnect (this=0x149f8060, pio=0x7fffa648b4ec, host=
{static npos = 18446744073709551615, _M_dataplus =
{<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No
data fields>}, <No data fields>}, _M_p = 0x7fffa648b440
"\030\200\237\024"}}, port=
{static npos = 18446744073709551615, _M_dataplus =
{<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No
data fields>}, <No data fields>}, _M_p = 0x7fffa648b450 "H
\200\237\024"}}, ph=0x7fffa648b4e0,
msg_register={len = 0, cmd = 32, res = 0}, msg_response={len = 0,
cmd = 33, res = 0}) at cconnect.cpp:17
17 ,m_is_informed
(-1)
(gdb) s
CInterfaceConnect (this=0x149f8060) at interfaceconnect.h:35
35 CInterfaceConnect(){}
(gdb) s
enable_shared_from_this (this=0x149f806c) at /root/boost_1_39_0/
boost/
smart_ptr/enable_shared_from_this.hpp:29
29 {
(gdb) s
weak_ptr (this=0x149f806c) at /root/boost_1_39_0/boost/smart_ptr/
weak_ptr.hpp:39
39 weak_ptr(): px(0), pn() // never throws in 1.30+
(gdb) s
weak_count (this=0x149f8074) at /root/boost_1_39_0/boost/smart_ptr/
detail/shared_count.hpp:313
313 weak_count(): pi_(0) // nothrow
(gdb) s
318 }
(gdb) s
weak_ptr (this=0x149f806c) at /root/boost_1_39_0/boost/smart_ptr/
weak_ptr.hpp:40
40 {
(gdb) s
basic_stream_socket (this=0x149f807c, io_service=@0x7fffa648b4ec)
at /root/boost_1_39_0/boost/asio/basic_stream_socket.hpp:70
70 : basic_socket<Protocol, StreamSocketService>(io_service)
(gdb) s
basic_socket (this=0x149f807c, io_service=@0x7fffa648b4ec)
at /root/boost_1_39_0/boost/asio/basic_socket.hpp:67
67 : basic_io_object<SocketService>(io_service)
(gdb) s
basic_io_object (this=0x149f807c, io_service=@0x7fffa648b4ec)
at /root/boost_1_39_0/boost/asio/basic_io_object.hpp:72
72 : service(boost::asio::use_service<IoObjectService>
(io_service))
(gdb) s
noncopyable (this=0x149f807c) at /root/boost_1_39_0/boost/
noncopyable.hpp:24
24 noncopyable() {}
(gdb) s
boost::asio::use_service<boost::asio::stream_socket_service<boost::asio::ip ::tcp>
> (ios=@0x7fffa648b4ec)

at /root/boost_1_39_0/boost/asio/impl/io_service.ipp:195
195 return ios.service_registry_->template use_service<Service>
();
(gdb) s
boost::asio::detail::service_registry::use_service<boost::asio::stream_sock
et_service<boost::asio::ip::tcp>

(this=0x149f3010) at /root/boost_1_39_0/boost/asio/detail/
service_registry.hpp:94
94 boost::asio::detail::mutex::scoped_lock lock(mutex_);
(gdb) s
scoped_lock (this=0x7fffa648b000, m=@0x149f3014) at /root/
boost_1_39_0/
boost/asio/detail/scoped_lock.hpp:34
34 : mutex_(m)
(gdb) s
noncopyable (this=0x7fffa648b000) at /root/boost_1_39_0/boost/
noncopyable.hpp:24
24 noncopyable() {}
(gdb) s
scoped_lock (this=0x7fffa648b000, m=@0x149f3014) at /root/
boost_1_39_0/
boost/asio/detail/scoped_lock.hpp:36
36 mutex_.lock();
(gdb) s
boost::asio::detail::posix_mutex::lock (this=0x149f3014)
at /root/boost_1_39_0/boost/asio/detail/posix_mutex.hpp:71
71 int error = ::pthread_mutex_lock(&mutex_);
(gdb) s
72 if (error != 0)
(gdb) s
80 }
(gdb) s
scoped_lock (this=0x7fffa648b000, m=@0x149f3014) at /root/
boost_1_39_0/
boost/asio/detail/scoped_lock.hpp:37
37 locked_ = true;
(gdb) s
boost::asio::detail::service_registry::use_service<boost::asio::stream_sock
et_service<boost::asio::ip::tcp>

(this=0x149f3010) at /root/boost_1_39_0/boost/asio/detail/
service_registry.hpp:97
97 boost::asio::io_service::service* service =
first_service_;
(gdb) s
98 while (service)
(gdb) s
100 if (service_id_matches(*service, Service::id))
(gdb) s
boost::asio::detail::service_registry::service_id_matches<boost::asio::stre
am_socket_service<boost::asio::ip::tcp>
> (service=@0x149f306000007fff) at /root/boost_1_39_0/boost/asio/

detail/service_registry.hpp:208
208 && *service.type_info_ == typeid
(typeid_wrapper<Service>);
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x000000000044c97a in
boost::asio::detail::service_registry::service_id_matches<boost::asio::stre
am_socket_service<boost::asio::ip::tcp>
> (service=@0x149f306000007fff)

at /root/boost_1_39_0/boost/asio/detail/service_registry.hpp:208
208 && *service.type_info_ == typeid
(typeid_wrapper<Service>);

浩毛

unread,
Oct 27, 2009, 8:56:16 AM10/27/09
to 高性能服务器研发与运营邮件列表
请问,为什么不用libevent呢?虽然没有将socket封装进来,但是select epool kqueue这些都已经封装了。整个代码结构很
简洁和优美。

Kouga

unread,
Oct 28, 2009, 5:23:51 AM10/28/09
to dev4s...@googlegroups.com
呵呵,Boost::asio的代码也比较优雅了~

这个段错误,我想先看看proxy.cpp:51 前面的初始化代码及类的构造函数及类定义。

之前我也遇到同样的问题,调整初始化顺序列表就对了。

2009/10/27 浩毛 <jax...@gmail.com>

请问,为什么不用libevent呢?虽然没有将socket封装进来,但是select epool kqueue这些都已经封装了。整个代码结构很
简洁和优美。






--
签名是什么东西??
Reply all
Reply to author
Forward
0 new messages