From 23f42da5486efa785157bd7f972f2c4fc40a0dde Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 11 Mar 2014 05:09:08 +0800 Subject: [PATCH] closed #4324: fix double free bug. move _preContactData to PhysicsContact class, add PhysicsContact::getPreContactData() method. --- cocos/physics/CCPhysicsContact.cpp | 16 +++++++--------- cocos/physics/CCPhysicsContact.h | 10 ++++------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/cocos/physics/CCPhysicsContact.cpp b/cocos/physics/CCPhysicsContact.cpp index 0f5967f9a7..f1ea5d5478 100644 --- a/cocos/physics/CCPhysicsContact.cpp +++ b/cocos/physics/CCPhysicsContact.cpp @@ -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(_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 && igetShapeA(), 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); diff --git a/cocos/physics/CCPhysicsContact.h b/cocos/physics/CCPhysicsContact.h index c6b574fca8..2ead410803 100644 --- a/cocos/physics/CCPhysicsContact.h +++ b/cocos/physics/CCPhysicsContact.h @@ -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;