类似于shared_ptr Assertion px != 0 failed
我正在编写一个游戏服务器,它产生一个新线程来处理每个用户会话。主线程有一个 std::vector 的 UserSession 共享指针。另一个线程定期从该向量中删除死会话,但在执行 std::vector::erase() 时失败。我无法找出我的生活出了什么问题。
错误是:
Prototype2: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access::type boost::shared_ptr::operator->() const [with T = UserSession; typename boost::detail::sp_member_access::type = UserSession*]: 断言 `px != 0' 失败。中止(核心转储)
相关代码为:
void GameServer::start()
{
int sessionid;
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port_));
boost::thread(&GameServer::session_monitor, this);
for (;;)
{
socket_shptr socket(new tcp::socket(io_service));
acceptor.accept(*socket);
sessionid = numsessions_++;
UserSession* usession = new
UserSession(socket, sessionid, io_service);
session_shptr session(usession);
sessions_mutex_.lock();
sessions_.push_back(session);
sessions_mutex_.unlock();
std::cout << "Starting session for client " <<
get_client_ip(*socket) << std::endl;
session->start();
}
}
void GameServer::session_monitor()
{
for (;;)
{
boost::this_thread::sleep(boost::posix_time::seconds(10));
std::cout << "Removing dead user sessions" << std::endl;
sessions_mutex_.lock();
for (std::vector<session_shptr>::iterator it = sessions_.begin();
it != sessions_.end(); ++it)
{
if ((*it)->is_dead())
{
std::cout << "Removing session: " << (*it)->id() <<
std::endl;
sessions_.erase(it);
}
}
sessions_mutex_.unlock();
}
}