I am using Cocos2d-x for a game that I port with Cocos2d-iphone. The original programmer, apparently, used the "function" of Objective-C to not crash when invoking Nile objects as a way to do a lot of messy things.
If this is due to the fact that I do not know, however, in my code I never call release () manually and, of course, do not delete anything like that either. I don’t even call → removeObject () at all (although this will not lead to the same problem as mine).
Now the problem: when the game is running, at random moments (they will not be random, but now they seem obvious), the child nodes get a NULL value. And this not only affects my code, but also Cocos2d internals. Example:
CCLog("----------------"); for(int j = 0; j < this->getChildren()->count(); j++) { CCObject *child = this->getChildren()->objectAtIndex(j); EnemySprite *enemy = dynamic_cast<EnemySprite*>(child); if (enemy != NULL) { CCLog("Enemy with tag %d found", enemy->getTag()); } } EnemySprite *enemy = dynamic_cast<EnemySprite*>(this->getChildByTag(i)); if (enemy == NULL) { CCLog("Now enemy with %d is NULL :(", i); }
In the getChildren () image, all enemies with tags are there and print this;
- Enemy with tag 1000 found
- Enemy with tag 1001
- Enemy with tag 1002
During the game, it will show it a lot until it shows it;
- Enemy with tag 1000 found
- Enemy with tag 1001
- Enemy with tag 1002
- Now the enemy with 1001 is NULL :(
and crashes.
In my mind, this should be impossible with the above code, since I just checked, checked and printed this particular object ...
But even more interesting (maybe only for me, maybe this is some kind of stupid mistake), this
this->getChildByTag(i)
accidentally mistaken inside; crossing the children, it will find NULL and exit Cocos2d's internal code:
if(pNode && pNode->m_nTag == aTag) return pNode;
Then pNode is not NULL (why statements do not run), but it looks like this:
http://o7.no/137JXC4 (screenshot)
The cocos2d :: CCCopying is already a nightmare for me in this project; every time I see this, I know that something is wrong, and I have no idea how to find what it is.
I already added a breakpoint in the line release () delete; it is not called. And I, as I said, does nothing of the sort manually.
I use Xcode / iOS for debugging, but the behavior on Android is the same (but on my computer, Eclipse is slower than Xcode, especially during debugging).
I understand that it would be difficult to give me a solution / reason, however; I would be very happy if someone tells me how to attack this problem. This happens by accident in the entire (rather large) code base, and I don’t understand how to find this problem ...
I hope someone can help!