3

I'm having problem with dynamic_cast. i just compiled my project and tested every thing in debug mode and then i tried compiling it in release mode, i have copied every configuration from debug mode exept optimization parameter which is now /o2, (while debuging i set it as /od) the project compiled but when it starts loading my resources i got exception in the piece of code here :

for(int j = 1; j < i->second->getParametersNumber();j++)
{
    CCTMXTiledMap* temp = CCTMXTiledMap::tiledMapWithTMXFile(i->second->As<string>(j).c_str());
    CCTMXLayer* ret = NULL;
    for(NSMutableArray<CCNode*>::NSMutableArrayIterator l=temp->getChildren()->begin();!ret && l!=temp->getChildren()->end();l++)
        ret = dynamic_cast<CCTMXLayer*> (*l);
    t1.first = ret;
    templates[i->first].second.push_back(t1);
    templates[i->first].second.back().first->retain();
}

nothing in code changed and when I check in debugger every variable in classes is what it should be but dynamic cast is throwing std::__non_rtti_object. what am i doing it wrong? and i'm using cocos2d-x ,I didn't have enough reputation to add that tag!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Ali1S232
  • 3,373
  • 2
  • 27
  • 46

2 Answers2

1

Does CCNode have any virtual functions? Are all elements of temp->getChildren()->begin() really CCNodes? Does temp->getChildren() return a reference? The latter is especially insidious: you call both temp->getChildren()->begin() and temp->getChildren()->end(). If getChildren() returns a copy, you're taking the begin of one copy and the end of another copy.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • it does have many virtual functions and i'm saying it word while in debug configuration if it didin't debug configuration would also made errors! – Ali1S232 Apr 07 '11 at 14:47
1

In this case after many code changes I found out there has to be some bugs which show themselves when code is optimized (still don't know if it's compiler's mis optimization or my code has some problems but it's probably mine). and the main reason for that problem was with *l being NULL.

Ali1S232
  • 3,373
  • 2
  • 27
  • 46