我有工作的 C++ 守护进程。问题是守护程序每月崩溃一到两次。正如您从下面的 GDB 输出中看到的那样,当守护程序在容器中搜索unsigned int
sessionID 时会发生这种情况。std::map <unsigned int const, SessionID*>
我无法重现该问题,并认为它可能来自用户的数据有问题(可能std::sting cookie_ssid
有一些意外数据,转换后strtoul
出现问题。(知道,这不是正确的获取方式unsigned int
)
.core
守护进程崩溃后我只有文件。并在if (!_M_impl._M_key_compare(_S_key(__x), __k))
. 任何想法如何解决这个问题?非常感谢。
GDB 输出:
#0 std::_Rb_tree<unsigned int, std::pair<unsigned int const, SessionID*>, std::_Select1st<std::pair<unsigned int const, SessionID*> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, SessionID*> > >::find (this=0x529df15c, __k=@0x7f5fab7c)
at stl_tree.h:1376
##########
1376 if (!_M_impl._M_key_compare(_S_key(__x), __k))
##########
#0 std::_Rb_tree<unsigned int, std::pair<unsigned int const, SessionID*>, std::_Select1st<std::pair<unsigned int const, SessionID*> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, SessionID*> > >::find (
this=0x529df15c, __k=@0x7f5fab7c) at stl_tree.h:1376
#1 0x0805e6be in TR::find_session (this=0x529df110,
cookie_ssid=@0x47ef3614, ptr_to_ptr_session=0x7f5fac7c)
at stl_map.h:542
函数TR::find_session
发布如下:
bool TR::find_session ( const std::string &cookie_ssid, SessionID **ptr_to_ptr_session )
{
unsigned int uint_sessionid = std::strtoul ( cookie_ssid.c_str(),NULL,0);
MUTEX_map_sessionids.lock_reading();
std::map<unsigned int, SessionID*>::iterator it_sessionids = map_sessionids.find( uint_sessionid );
if ( it_sessionids != map_sessionids.end() )
{ // exists
*ptr_to_ptr_session = it_sessionids->second;
MUTEX_map_sessionids.unlock();
return true;
}
MUTEX_map_sessionids.unlock();
return false;
}
编辑 我的清理功能,在分离线程(每分钟一次或 5 分钟)工作。根据评论的要求。我不确定这个功能。也许它的越野车......
void TR::cleanup_sessions () // not protected from multithread using! used only at one thread
{
std::list<SessionID*> list_to_clean; // tmplary store sessions to delete
MUTEX_map_sessionids.lock_reading();
std::map<unsigned int, SessionID*>::iterator it_sessionids = map_sessionids.begin();
MUTEX_map_sessionids.unlock();
while ( true )
{
MUTEX_map_sessionids.lock_writing();
if (it_sessionids == map_sessionids.end() )
{
MUTEX_map_sessionids.unlock();
break;
}
SessionID *ptr_sessionid = it_sessionids->second;
time_t secondsnow = time (NULL);
ptr_sessionid->MUTEX_all_session.lock_reading();
time_t lastaccesstime = ptr_sessionid->last_access_time;
size_t total_showed = ptr_sessionid->map_showed.size();
ptr_sessionid->MUTEX_all_session.unlock();
if ( lastaccesstime and secondsnow - lastaccesstime > LOCALSESSION_LIFETIME_SEC ) // lifetime end!
{
// delete session from map
map_sessionids.erase( it_sessionids++ ); // Increments the iterator but returns the original value for use by erase
MUTEX_map_sessionids.unlock();
list_to_clean.push_back ( ptr_sessionid ); // at the end
}
else if ( total_showed == 0 and secondsnow - lastaccesstime > 36000 ) // not active for N secontes
{
map_sessionids.erase( it_sessionids++ ); // Increments the iterator but returns the original value for use by erase
MUTEX_map_sessionids.unlock();
// add pointer to list to delete it latter
list_to_clean.push_back ( ptr_sessionid ); // at the end
}
else
{
++it_sessionids; // next
MUTEX_map_sessionids.unlock();
}
}
// used? pause
if ( !list_to_clean.empty() )
{
//sleep(1);
}
// cleanup session deleted from working map
while ( !list_to_clean.empty() )
{
SessionID *ptr_sessionid_to_delete = list_to_clean.front();
list_to_clean.pop_front();
ptr_sessionid_to_delete->MUTEX_all_session.lock_writing(); // protected lock session mutex. can not delete session if its already locked. (additational protection)
ptr_sessionid_to_delete->cleanup();
delete ptr_sessionid_to_delete;
}
}
注意,正如您在每次迭代中看到的那样,我锁定/解锁 map_sessions,因为此时其他线程会查找/插入新会话及其关键,因为用户不能等待。