此代码在崩溃之前运行了100,000次(在测试中运行6小时)。当崩溃发生时,它会在shared_ptr超出范围时发生...此函数的目的是填充传递的shared_ptr向量(在本例中),消息按类型过滤...所以不是historyQueue中的所有消息得到补充。这个新填充的向量稍后用于发送这些PM ...这个函数也被其他线程调用,这就是为什么有一个LockGuard,它是标准lockguard的typedef(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;
}
回溯:
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
相关部分函数调用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);
} }
此后不再使用向量lp ...
任何帮助都将非常感谢....谢谢
答案 0 :(得分:1)
我唯一的预感是你在其他地方UB。
它可能是您作为参考传递的向量上的竞赛(v
)。很容易忘记同步访问权限。
稍微偏离主题:我可以建议一点简化。
减少代码/分离问题总是有助于保持复杂性,从而有助于降低错误率:
bool getMessages(vector<PM> &v, Query const& query)
{
LockGuard lock(historyMutex);
v.clear();
std::copy_if(historyQueue.begin(), historyQueue.end(), back_inserter(v), query);
return true;
}
那里有很多混乱/冲突的事情:
条件
if (count >= v.size()) {
break;
}
由于前面的resize()
永远不会成真 之后
v.resize(count)
......这只是问copy_if
(实际上,更好的是v.assign_if
但是图书馆没有想到这一点.Boost Range会做它)。
这里有很多混合和冗余:
if (!pm->GetPid() || !pm->GetMid()) // 1.
continue;
就在这里,我们已经知道pmtype
不能为零:
pmtype = (pm->GetPid() << 16) + pm->GetMid();
if (filter && pmtype) // pmtype cannot be zero
pmok = filter->messageIsOk(pmtype);
pmok
现在已部分定义,仅在filter!=0
...然后在使用中变得毛茸茸:
if ((allMessages || uid == -1 || (playerFilter && uid == playerId)) && (filter == 0 || pmok)) {
显然更容易做
bool pmok = !filter || filter->messageIsOk(pmtype);
if ((allMessages || uid == -1 || (playerFilter && uid == playerId)) && pmok) {
我认为allMessages
没有按照说法行事,这是不确定的。它实际上与playerFilter==false
现在实现的目标相反。我认为你可能错放了那里的括号, 或 你应该调用旗帜allUserIds
? (我假设后者)。
您可以使用optional<int> playerId
简化界面。但是,由于显然uid==-1
显然是一个神奇的价值,为什么不让-1
信号'没有用户ID过滤器'呢?
什么是for_vector
(当然......编写循环不是一个宏?再次,这就是copy_if
的用途)。
使用这些调整,可以完全实现Query类:
struct Query {
bool allPlayers;
int playerId;
MessageFilter *filter;
Query(bool allPlayers = true, int playerId = -1, MessageFilter* filter = nullptr) :
allPlayers(allPlayers), playerId(playerId), filter(filter)
{
}
bool operator()(PM const& pm) const {
if (!(pm->GetPid() && pm->GetMid()))
return false;
int pmtype = (pm->GetPid() << 16) + pm->GetMid();
bool pmok = !filter || filter->messageIsOk(pmtype);
int uid = pm->GetPlayerDest();
bool matchPlayer = uid == -1 || allPlayers || (playerId != -1 && uid == playerId);
return pmok && matchPlayer;
}
};
使用就像是。
std::vector<MessageHistory::PM> v;
hist.getMessages(v, MessageHistory::Query { false, 42 }); // only uid 42; no MessageFilter
<强> Live On Coliru 强>
#include <vector>
#include <mutex>
#include <memory>
#include <deque>
#include <algorithm>
using LockGuard = std::lock_guard<std::mutex>;
std::mutex historyMutex;
struct ProtocolMessage { //stub
int GetPlayerDest() const { return 1; }
int GetPid() const { return 1; }
int GetMid() const { return 1; }
};
struct MessageFilter {
virtual bool messageIsOk(int) const { return true; }
virtual ~MessageFilter() { }
};
struct MessageHistory
{
using PM = std::shared_ptr<ProtocolMessage>;
struct Query {
bool allPlayers;
int playerId;
MessageFilter *filter;
Query(bool allPlayers = true, int playerId = -1, MessageFilter* filter = nullptr) :
allPlayers(allPlayers), playerId(playerId), filter(filter)
{
}
bool operator()(PM const& pm) const {
if (!(pm->GetPid() && pm->GetMid()))
return false;
int pmtype = (pm->GetPid() << 16) + pm->GetMid();
bool pmok = !filter || filter->messageIsOk(pmtype);
int uid = pm->GetPlayerDest();
bool matchPlayer = uid == -1 || allPlayers || (playerId != -1 && uid == playerId);
return pmok && matchPlayer;
}
};
bool getMessages(std::vector<PM> &v, Query const& query)
{
LockGuard lock(historyMutex);
v.clear();
std::copy_if(historyQueue.begin(), historyQueue.end(), back_inserter(v), query);
return true;
}
private:
std::deque<PM> historyQueue;
};
int main() {
MessageHistory hist;
std::vector<MessageHistory::PM> v;
hist.getMessages(v, MessageHistory::Query { false, 42 }); // only uid 42; no MessageFilter
}