Browse Source

Fix unsafe cleanup in ~plCameraRegionDetector

Deleting an hsRefCnt is evil, we should ALWAYS hsRefCnt_SafeUnRef (we
might have a nullptr) it because someone might ref it. hsRefCnt will explictly crash if you delete it when the refcnt != 1.

Also, take this opportunity to vector-ize things.
Adam Johnson 13 years ago
parent
commit
9795b4e7fc
  1. 25
      Sources/Plasma/PubUtilLib/plPhysical/plCollisionDetector.cpp
  2. 6
      Sources/Plasma/PubUtilLib/plPhysical/plCollisionDetector.h

25
Sources/Plasma/PubUtilLib/plPhysical/plCollisionDetector.cpp

@ -236,13 +236,8 @@ void plCollisionDetector::Write(hsStream* stream, hsResMgr* mgr)
plCameraRegionDetector::~plCameraRegionDetector() plCameraRegionDetector::~plCameraRegionDetector()
{ {
for(int i = 0; i < fMessages.Count(); i++) for (plCameraMsgVec::iterator it = fMessages.begin(); it != fMessages.end(); ++it)
{ hsRefCnt_SafeUnRef(*it);
plMessage* pMsg = fMessages[i];
fMessages.Remove(i);
delete(pMsg);
}
fMessages.SetCountAndZero(0);
} }
void plCameraRegionDetector::ITrigger(plKey hitter, bool entering, bool immediate) void plCameraRegionDetector::ITrigger(plKey hitter, bool entering, bool immediate)
@ -314,7 +309,7 @@ void plCameraRegionDetector::ISendSavedTriggerMsgs()
{ {
if (fSavingSendMsg) if (fSavingSendMsg)
{ {
for (int i = 0; i < fMessages.Count(); i++) for (size_t i = 0; i < fMessages.size(); ++i)
{ {
char str[256]; char str[256];
@ -322,13 +317,13 @@ void plCameraRegionDetector::ISendSavedTriggerMsgs()
if (fSavedMsgEnterFlag) if (fSavedMsgEnterFlag)
{ {
fMessages[i]->SetCmd(plCameraMsg::kEntering); fMessages[i]->SetCmd(plCameraMsg::kEntering);
sprintf(str, "Entering cameraRegion: %s - Evals=%d -msg %d of %d\n", GetKeyName().c_str(),fNumEvals,i+1,fMessages.Count()); sprintf(str, "Entering cameraRegion: %s - Evals=%d -msg %d of %d\n", GetKeyName().c_str(),fNumEvals,i+1,fMessages.size());
fIsInside = true; fIsInside = true;
} }
else else
{ {
fMessages[i]->ClearCmd(plCameraMsg::kEntering); fMessages[i]->ClearCmd(plCameraMsg::kEntering);
sprintf(str, "Exiting cameraRegion: %s - Evals=%d -msg %d of %d\n", GetKeyName().c_str(),fNumEvals,i+1,fMessages.Count()); sprintf(str, "Exiting cameraRegion: %s - Evals=%d -msg %d of %d\n", GetKeyName().c_str(),fNumEvals,i+1,fMessages.size());
fIsInside = false; fIsInside = false;
} }
plgDispatch::MsgSend(fMessages[i]); plgDispatch::MsgSend(fMessages[i]);
@ -372,8 +367,8 @@ void plCameraRegionDetector::Read(hsStream* stream, hsResMgr* mgr)
{ {
plDetectorModifier::Read(stream, mgr); plDetectorModifier::Read(stream, mgr);
int n = stream->ReadLE32(); int n = stream->ReadLE32();
fMessages.SetCountAndZero(n); fMessages.resize(n);
for(int i = 0; i < n; i++ ) for(size_t i = 0; i < n; i++ )
{ {
plCameraMsg* pMsg = plCameraMsg::ConvertNoRef(mgr->ReadCreatable(stream)); plCameraMsg* pMsg = plCameraMsg::ConvertNoRef(mgr->ReadCreatable(stream));
fMessages[i] = pMsg; fMessages[i] = pMsg;
@ -383,9 +378,9 @@ void plCameraRegionDetector::Read(hsStream* stream, hsResMgr* mgr)
void plCameraRegionDetector::Write(hsStream* stream, hsResMgr* mgr) void plCameraRegionDetector::Write(hsStream* stream, hsResMgr* mgr)
{ {
plDetectorModifier::Write(stream, mgr); plDetectorModifier::Write(stream, mgr);
stream->WriteLE32(fMessages.GetCount()); stream->WriteLE32(fMessages.size());
for(int i = 0; i < fMessages.GetCount(); i++ ) for(plCameraMsgVec::iterator it = fMessages.begin(); it != fMessages.end(); ++it)
mgr->WriteCreatable( stream, fMessages[i] ); mgr->WriteCreatable( stream, *it );
} }
void plCameraRegionDetector::IHandleEval(plEvalMsg *pEval) void plCameraRegionDetector::IHandleEval(plEvalMsg *pEval)

6
Sources/Plasma/PubUtilLib/plPhysical/plCollisionDetector.h

@ -177,7 +177,9 @@ public:
class plCameraRegionDetector : public plObjectInVolumeDetector class plCameraRegionDetector : public plObjectInVolumeDetector
{ {
protected: protected:
hsTArray<plCameraMsg*> fMessages; typedef std::vector<plCameraMsg*> plCameraMsgVec;
plCameraMsgVec fMessages;
bool fIsInside; bool fIsInside;
bool fSavingSendMsg; bool fSavingSendMsg;
bool fSavedMsgEnterFlag; bool fSavedMsgEnterFlag;
@ -193,7 +195,7 @@ public:
~plCameraRegionDetector(); ~plCameraRegionDetector();
virtual hsBool MsgReceive(plMessage* msg); virtual hsBool MsgReceive(plMessage* msg);
void AddMessage(plCameraMsg* pMsg) { fMessages.Append(pMsg); } void AddMessage(plCameraMsg* pMsg) { fMessages.push_back(pMsg); }
CLASSNAME_REGISTER( plCameraRegionDetector ); CLASSNAME_REGISTER( plCameraRegionDetector );
GETINTERFACE_ANY( plCameraRegionDetector, plCollisionDetector ); GETINTERFACE_ANY( plCameraRegionDetector, plCollisionDetector );

Loading…
Cancel
Save