【发布时间】:2017-08-16 13:05:44
【问题描述】:
我正在运行一些小型在线游戏,并且有时服务器崩溃。我找不到导致 SIGSEGV 的原因。 gdb 把我带到那个功能:
bool Player::getStorageValue(const uint32_t key, int32_t& value) const
{
auto it = storageMap.find(key);
if (it == storageMap.end()) {
value = -1;
return false;
}
value = it->second;
return true;
}
我能否以某种方式更好地保护它,不让程序尝试访问内存的受限区域?
这是我的 gdb 日志:
(gdb) bt full
#0 0x000000000054503c in std::less<unsigned int>::operator()(unsigned int const&, unsigned int const&) const ()
No symbol table info available.
#1 0x0000000000740dca in std::_Rb_tree<unsigned int, std::pair<unsigned int const, int>, std::_Select1st<std::pair<unsigned int const, int> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, int> > >::_M_lower_bound(std::_Rb_tree_node<std::pair<unsigned int const, int> > const*, std::_Rb_tree_node<std::pair<unsigned int const, int> > const*, unsigned int const&) const ()
No symbol table info available.
#2 0x000000000073e145 in std::_Rb_tree<unsigned int, std::pair<unsigned int const, int>, std::_Select1st<std::pair<unsigned int const, int> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, int> > >::find(unsigned int const&) const ()
No symbol table info available.
#3 0x000000000073c46f in std::map<unsigned int, int, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, int> > >::find(unsigned int const&) const ()
No symbol table info available.
#4 0x000000000072dfa2 in Player::getStorageValue(unsigned int, int&) const ()
No symbol table info available.
#5 0x00000000006b2890 in LuaScriptInterface::luaPlayerGetStorageValue(lua_State*) ()
我已将互斥锁添加到代码中,以防止线程同时尝试某些元素
bool Player::getStorageValue(const uint32_t key, int32_t& value) const
{
std::lock_guard<std::mutex> lockClass(mutex_gp13);
auto it = storageMap.find(key);
if (it == storageMap.end()) {
value = -1;
return false;
}
value = it->second;
return true;
}
虽然这应该可以解决我的问题,但今天与那个 gdb 日志有相同的 SIGSEGV。
编辑: 我发现我没有将互斥锁放在写入地图的函数上。我现在要试试这个。
【问题讨论】:
-
您的地图似乎已损坏。这里的代码没问题。
标签: c++ gdb stdmap segmentation-fault