@Kreacher wrote:
In my game, I have a custom class,
Hero
, that is derived fromNode
, and has a custom class,StateMachine
to manage its state.The
StateMachine
is derived fromcocos2d::Ref
, so that I can use Cocos' automatic ref counting to manage its memory.At the end of the
Hero
'sinit
function, itsStateMachine
is correctly initialized and has a_refCount
of 2, which reduces to 1 at the end of the first frame, which makes sense because theAutoReleasePool
gets cleared then.However, I'm finding that when the
Hero
's move function is invoked, itsStateMachine
variable points to a random new memory address, in which XCode debugger tells me has empty variables etc.Here are some relevant code snippets:
// GameScene.hpp class GameScene : public cocos2d::Layer { private: // ... Hero* m_hero; public: // ... } // GameScene.cpp bool GameScene::init(const ValueMap& configuration) { // ... other config m_hero = Hero::create(refNode, heroConfig); if(!m_hero) { FATAL("Could not create a Hero!"); return false; } m_hero->retain(); // other stuff return true; } GameScene::onExit() { // ... CC_SAFE_RELEASE(m_hero); // ... }
// Hero.hpp class Hero : public Node { private: // ... StateMachine* m_stateMachine; public: static Hero* create(Node* heroNode, const ValueMap& configuration); // ... } // Hero.cpp Hero* Hero::create(Node* heroNode, const ValueMap& configuration) { Hero* hero = static_cast<Hero*>(heroNode); if(hero && hero->init(configuration)) { hero->autorelease(); } else { CC_SAFE_DELETE(hero); } return hero; } bool Hero::init(const ValueMap& configuration) { // ... m_stateMachine = StateMachine::create("HeroState"); if(!m_stateMachine) { FATAL("Could not create a StateMachine!"); return false; } m_stateMachine->retain(); // ... // breakpoint here reveals: // m_stateMachine->_ID == 1042 // m_stateMachine->_referenceCount == 2 return true; } void Hero::moveToPosition(const Vec2& targetPosition) { // breakpoint here reveals: // m_stateMachine points to a different memory address // m_stateMachine->_ID == some random number // m_stateMachine->_referenceCount == some random number, but usually 256 // never goes here because `isInState` returns false always, since this StateMachine // hasn't been initialized if(m_stateMachine->isInState(IDLE)) {...} }
Also, using XCode debugger, during the
Hero
'sinit
function, if I useexpr m_stateMachine
to get a reference to theStateMachine
, and then later check this reference, it still exists, but its_ID
and_referenceCount
are also similarly changed, and the instance ofHero
has lost the reference to it.Here's a screenshot of that flow, the
this
that is referenced is the instance ofHero
:I have other modules that contain
StateMachine
s but they don't seem to have any of these issues.Any idea what is going on here? Or do you have any suggestions to point me in the right direction?
Sidenote:
Also, I noticed that XCode thinks of the
Hero
object pointer as aNode*
in the debugger variables view. Additionally, I've noticed that even though I have overridenNode
'svirtual void update(float delta)
method, invokingm_hero->update(...)
inGameScene
callsNode
'supdate
. Which is wierd, becausem_hero
is aHero*
pointer. I got around that by creating an update function of a different name and using that, but I'm wondering if that's related to this issue
Posts: 4
Participants: 3