2017-07-06 35 views
0

在Xcode 8.3.3,C++ 11中内置的调试版本之间的行为出现意外差异Cocos2d-x框架。我使用emplace()方法来填充std :: map(_randomLetterChancesToAppear),它正确地在调试中构建,但似乎不会在发布中填充。这种说法在同一时间被绊倒当两个值预期等于:调试和发布版本之间std :: map行为的差异(Xcode 8.3,C++ 11,Cocos2d-x)

CC_ASSERT(outCellDetail._randomLetterChancesToAppear.size() == alphabetCount); 

当一个发布版本的断言人次,_randomLetterChancesToAppear似乎并没有包含任何数据。 (为了测试这个,在相关的情况下,我创建了一个重复的Xcode方案到我的默认移动方案,并且将Run build配置更改为发布以允许我测试具有附加IDE的版本构建)。这里是有问题的方法:

void GameDataController::PopulateIndividualCellChancesToAppear(CellDetail &outCellDetail, const rapidjson::Value &cellDetailObject, const GameDetail& constGameDetail) const 
{ 
    cocostudio::DictionaryHelper* dicTool = cocostudio::DictionaryHelper::getInstance(); 
    CC_ASSERT(dicTool->checkObjectExist_json(cellDetailObject, "LetterChancesToAppear")); 
    CCASSERT(constGameDetail._alphabet.length() > 0, "Zero-length alphabet!"); 

    const rapidjson::Value& letterChancesObject = dicTool->getSubDictionary_json(cellDetailObject, "LetterChancesToAppear"); 

    // iterate through each letter in this game detail's alphabet 
    const int alphabetCount = static_cast<int>(constGameDetail._alphabet.length()); 
    std::vector<char> lettersNeedingChances; 
    for (int i = 0; i < alphabetCount; ++i) 
    { 
     // if a given alphabet letter has a chance to appear, save it. 
     const char alphabetChar = constGameDetail._alphabet.at(i); 
     char letterChanceKey[1]; 
     sprintf(letterChanceKey, "%c", alphabetChar); 
     if (dicTool->checkObjectExist_json(letterChancesObject, letterChanceKey)) 
     { 
      const float ChanceToAppear = dicTool->getFloatValue_json(letterChancesObject, letterChanceKey); 
      CC_ASSERT(!std::isnan(ChanceToAppear)); 
      CC_ASSERT(ChanceToAppear >= 0.0f); 
      outCellDetail._randomLetterChancesToAppear.emplace(alphabetChar, !std::isnan(ChanceToAppear) ? ChanceToAppear : 0.0f); 
     } 
     else 
     { 
      // ... otherwise, store the letter in a list of letters without assigned chances. 
      lettersNeedingChances.push_back(alphabetChar); 
     } 
    } 

    // fill in missing chances to appear. 
    if (lettersNeedingChances.size() > 0) 
    { 
     float defaultRemainingChance = 1.0f/static_cast<float>(alphabetCount); 
     CC_ASSERT(!std::isnan(defaultRemainingChance)); 
     CC_ASSERT(defaultRemainingChance > FLT_EPSILON); 
     for (const char remainingChar : lettersNeedingChances) 
     { 
      CCLOG("Character %c doesn't have a chance to appear specified. Using default %f", remainingChar, defaultRemainingChance); 
      outCellDetail._randomLetterChancesToAppear.emplace(remainingChar, defaultRemainingChance); 
     } 
    } 
    // vvvv THIS ASSERT IS FAILING IN RELEASE, BUT NOT IN DEBUG BUILD CONFIGURATIONS. vvvvv 
    CC_ASSERT(outCellDetail._randomLetterChancesToAppear.size() == alphabetCount); 

    // normalize chances to appear. 
    float chanceTotal = 0.0f; 
    for(auto iterator = outCellDetail._randomLetterChancesToAppear.begin(); 
     iterator != outCellDetail._randomLetterChancesToAppear.end(); 
     iterator++) 
    { 
     chanceTotal += iterator->second; 
    } 

    if (std::abs(chanceTotal - 1.0f) > FLT_EPSILON) 
    { 
     float adjustmentFactor = 1.0f/chanceTotal; 
     float adjustedChanceTotal = 0.0f; 
     for(auto iterator = outCellDetail._randomLetterChancesToAppear.begin(); 
      iterator != outCellDetail._randomLetterChancesToAppear.end(); 
      iterator++) 
     { 
      iterator->second = iterator->second * adjustmentFactor; 
      adjustedChanceTotal += iterator->second; 
     } 
     CCASSERT(std::abs(adjustedChanceTotal - 1.0f) <= FLT_EPSILON, "adjustedChanceTotal != 1.0f"); 
    } 
} 

为什么这种方法可能在两个构建配置之间表现不同有什么想法?

+0

他可能是未定义的行为又名。 UB。你应该尝试使用像valgrind这样的工具,Xcode可能有类似的东西。这可以告诉你,例如:如果你正在使用未初始化的内存或写入未分配的内存。有许多其他工具可用于寻找此类错误。 – Jonas

+1

'char letterChanceKey [1]; sprintf(letterChanceKey,“%c”,alphabetChar);'这会通过缓冲区溢出展现未定义的行为。 'letterChanceKey'没有空间用于终止NUL。 –

+0

非常感谢伊戈尔!增加缓冲区大小似乎解决了问题。 –

回答

0

积分为Igor Tandetnik为答案。

char letterChanceKey[1]; 
    sprintf(letterChanceKey, "%c", alphabetChar); 

没有留下足够的空间用于终止NUL并导致缓冲区溢出。增加缓冲区大小解决了问题。

谢谢伊戈尔!

相关问题