jeudi 26 février 2015

c++ Crash when shared_ptr goes out of scope...after running at least 100,000 times successfully


This code has run 100,000's of times before crashing around (6hrs of running in tests). When the crash happens it occurs when shared_ptr goes out of scope...the purpose of this function is to fill the passed in vector of shared_ptr ( in this case), with the messages filtered by type...So not all messages in historyQueue get added. this newly filled vector is used later to send these PMs...this function is also called by other threads which is why there is a LockGuard which is typedef for standard lockguard (typedef std::lock_guard LockGuard)



bool MessageHistory::getMessages(vector< shared_ptr<ProtocolMessage> >& v,bool allMessages, bool playerFilter, int playerId, MessageFilter* filter)
{
LockGuard lock(historyMutex);
v.resize(historyQueue.size());
unsigned count=0;

for_vector(historyQueue,i)
{ PM pm=historyQueue[i];//PM is a shared_ptr as well items in histortQueue
const int uid=pm->GetPlayerDest();
bool pmok =false;
int pmtype=0;



if(!pm->GetPid() || !pm->GetMid())
continue;

pmtype=(pm->GetPid() << 16) + pm->GetMid();



if(filter && pmtype)
pmok=filter->messageIsOk(pmtype);

if ((allMessages || uid == -1 || (playerFilter && uid == playerId))
&& (filter == 0 || pmok))
{ if(count>=v.size())
{ break;
}
v[count++]=pm;
}

}//crash happens here after 100,000's of successful calling of this function
v.resize(count);
return true;
}


backtrace:



Program terminated with signal 11, Segmentation fault.
#0 0x00007f74546b51b0 in ?? ()
(gdb) bt
#0 0x00007f74546b51b0 in ?? ()
#1 0x00000000005a7f41 in _M_release (this=0x7f7454204230)
at /usr/include/c++/4.6/bits/shared_ptr_base.h:146
#2 ~__shared_count (this=0x7f747dff0d38, __in_chrg=<optimized out>)
at /usr/include/c++/4.6/bits/shared_ptr_base.h:551
#3 ~__shared_ptr (this=0x7f747dff0d30, __in_chrg=<optimized out>)
at /usr/include/c++/4.6/bits/shared_ptr_base.h:751
#4 ~shared_ptr (this=0x7f747dff0d30, __in_chrg=<optimized out>)
at /usr/include/c++/4.6/bits/shared_ptr.h:93
#5 MessageHistory::getMessages (this=0x7e3cf18, v=..., allMessages=false,
playerFilter=true, playerId=-2141, filter=0x7e3cf88)
at MessageHistory.cpp:177


relevant part function calling getmessages :



if (handIsActive.IsLocked() && history.size() > 0)
{ vector< shared_ptr<ProtocolMessage> > lp;
history.getMessages(lp,playerId,&noChatFilter);
shared_ptr<ProtocolMessage> pm(new HandSoFar(lp));
GameQueue::sendMessage(address, pm);
} }


the vector lp is not used again after this...


any help would be greatly appreciated....thanks




Aucun commentaire:

Enregistrer un commentaire