Hi all,
I believe I might have stumbled upon a possible issue in the following routine in EAGLView.mm. This source has been taken from the stable release cocos2d-0.99.5-x-0.8.1.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
cocos2d::NSSet set;
for (UITouch *touch in touches) {
NSNumber *index = (NSNumber*)CFDictionaryGetValue(touchesIntergerDict, touch);
if (! index) {
// if the index doesn't exist, it is an error
return;
}
cocos2d::CCTouch *pTouch = s_pTouches[[index intValue]];
if (! pTouch) {
// if the pTouch is null, it is an error
return;
}
float x = [touch locationInView: [touch view]].x;
float y = [touch locationInView: [touch view]].y;
int tapCount = [touch tapCount];
pTouch->SetTouchInfo(0, tapCount, x, y);
set.addObject(pTouch);
CFDictionaryRemoveValue(touchesIntergerDict, touch);
pTouch->release(); //Could this lead to a dangling pointer?
s_pTouches[[index intValue]] = NULL;
[self removeUsedIndexBit:[index intValue]];
}
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesEnded(&set);
}
Since the std::set inserts a pointer to a CCTouch, it doesn’t create a copy of the CCTouch data. I came across this issue while doing the following:
void MainGame::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
if(touches->count() == 1)
{
CCTouch *const touch1 = dynamic_cast(touches->anyObject());
assert(touch1);
CCTouch *const touch2 = (CCTouch *)touches->anyObject();
....
}
}
The dynamic_cast for touch1 would fail but the C-Style cast for touch2 would succeed but with undefined results.