2017-03-23 15 views
0

私はいくつかの小さなオンラインゲームを実行しており、しばらくの間にサーバークラッシュがあります。私はSIGSEGVの原因を見つけることができません。 gdbは私をその機能に連れて行きます:find関数を使用するときにstd :: mapでSIGSEGVを取得する

制限付きメモリ領域にアクセスしようとしないようにしてもよろしいですか?私は、同時にスレッドがいくつかの要素にしようとしてはいけないし、コードにミューテックスを追加しました

(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; 
} 

私はそれがすべきかかわら:

そしてHERESに私のgdbのログ私の問題を解決しましたが、今日はそのgdbログと同じSIGSEGVを持っていました。

編集: マップに書き込む関数にmutexを置かなかったことがわかりました。私は今これで試してみるつもりです。

+0

地図が壊れているようです。ここのコードは大丈夫です。 –

答えて

1

これは私があなたがマルチスレッドであるかもしれないと思っているサーバーだとします。この場合、イテレータを無効にする戻り値を設定する前に、マップがどこかで変更されている可能性があります。その後、無効なイテレータにアクセスすると、プログラムはひどく死ぬ。

この場合、セマフォなどで変更しないように保護する必要があります。もしそうでなければ、おそらくもっと良い情報が必要になるでしょう。

+0

主な問題は、あるスレッドが同時にいくつかの要素を編集する可能性があるということですか?そこにミューテックスを入れてみます。 – Piodo

+0

私はそれが大丈夫だと思った、エラーは2日間発生しませんでしたが、今日私はちょうど同じgdbログを持っていた。ミューテックスはここで私を助けなかった、その奇妙な。 – Piodo

+0

私は適切にミューテックスを動作させていないことがわかりました。より良いものを試してみましょう – Piodo

関連する問題