Merge pull request #5662 from boyu0/bug4324_physicscontact_memory_leak

closed #4324: fix double free bug. move _preContactData to PhysicsContact class, add PhysicsContact::getPreContactData() method.
This commit is contained in:
James Chen 2014-03-11 10:48:15 +08:00
commit 3622b03c7b
2 changed files with 11 additions and 15 deletions

View File

@ -44,11 +44,11 @@ PhysicsContact::PhysicsContact()
, _eventCode(EventCode::NONE)
, _info(nullptr)
, _notificationEnable(true)
, _begin(false)
, _result(true)
, _data(nullptr)
, _contactInfo(nullptr)
, _contactData(nullptr)
, _preContactData(nullptr)
{
}
@ -57,6 +57,7 @@ PhysicsContact::~PhysicsContact()
{
CC_SAFE_DELETE(_info);
CC_SAFE_DELETE(_contactData);
CC_SAFE_DELETE(_preContactData);
}
PhysicsContact* PhysicsContact::construct(PhysicsShape* a, PhysicsShape* b)
@ -96,7 +97,8 @@ void PhysicsContact::generateContactData()
}
cpArbiter* arb = static_cast<cpArbiter*>(_contactInfo);
CC_SAFE_DELETE(_contactData);
CC_SAFE_DELETE(_preContactData);
_preContactData = _contactData;
_contactData = new PhysicsContactData();
_contactData->count = cpArbiterGetCount(arb);
for (int i=0; i<_contactData->count && i<PhysicsContactData::POINT_MAX; ++i)
@ -108,15 +110,13 @@ void PhysicsContact::generateContactData()
}
// PhysicsContactPreSolve implementation
PhysicsContactPreSolve::PhysicsContactPreSolve(PhysicsContactData* data, void* contactInfo)
: _preContactData(data)
, _contactInfo(contactInfo)
PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo)
: _contactInfo(contactInfo)
{
}
PhysicsContactPreSolve::~PhysicsContactPreSolve()
{
CC_SAFE_DELETE(_preContactData);
}
float PhysicsContactPreSolve::getRestitution() const
@ -217,7 +217,6 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
if (onContactBegin != nullptr
&& hitTest(contact->getShapeA(), contact->getShapeB()))
{
contact->_begin = true;
contact->generateContactData();
ret = onContactBegin(*contact);
}
@ -232,8 +231,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
if (onContactPreSolve != nullptr
&& hitTest(contact->getShapeA(), contact->getShapeB()))
{
PhysicsContactPreSolve solve(contact->_begin ? nullptr : contact->_contactData, contact->_contactInfo);
contact->_begin = false;
PhysicsContactPreSolve solve(contact->_contactInfo);
contact->generateContactData();
ret = onContactPreSolve(*contact, solve);

View File

@ -78,6 +78,8 @@ public:
inline PhysicsShape* getShapeB() const { return _shapeB; }
/** get contact data */
inline const PhysicsContactData* getContactData() const { return _contactData; }
/** get previous contact data */
inline const PhysicsContactData* getPreContactData() const { return _preContactData; }
/** get data. */
inline void* getData() const { return _data; }
/**
@ -112,12 +114,12 @@ private:
EventCode _eventCode;
PhysicsContactInfo* _info;
bool _notificationEnable;
bool _begin;
bool _result;
void* _data;
void* _contactInfo;
PhysicsContactData* _contactData;
PhysicsContactData* _preContactData;
friend class EventListenerPhysicsContact;
friend class PhysicsWorldCallback;
@ -146,14 +148,10 @@ public:
void ignore();
private:
PhysicsContactPreSolve(PhysicsContactData* data, void* contactInfo);
PhysicsContactPreSolve(void* contactInfo);
~PhysicsContactPreSolve();
private:
float _elasticity;
float _friction;
Point _surfaceVelocity;
PhysicsContactData* _preContactData;
void* _contactInfo;
friend class EventListenerPhysicsContact;