不相关的变量初始化导致代码破坏c ++

时间:2017-03-24 13:55:31

标签: c++ heisenbug

长话短说:我编写了一个使用yaml-cpp库来解析YAML文件的模板化函数。此文件包含特定格式的参数列表。该函数返回由参数name参数指定的任意类型的必需参数(因此模板化)。

该功能正常。我使用它来获取main()中的一些参数,如下所示:

int main(int argc, char** argv)
{
const std::string config_file_path = "/home.net/aa17gil/test/test.yaml";
const std::string param_name = "trolley/lasers/laserHoriz/min_rate";
const std::string param_name2 = "software/nodes/slam_karto/max_allowed_load_ratio";
//const std::string param_name3 = "software/nodes/slam_karto/unwanted_topics";

double min_rate;
double ratio;

//const std::string blah = "wtf";

if (getParamFromConfigFile<double>(config_file_path, param_name, min_rate))
    std::cout << "min_rate = " << min_rate << std::endl;
else
    std::cout << "Could not read param from config file" << std::endl;

if (getParamFromConfigFile<double>(config_file_path, param_name2, ratio))
    std::cout << "ratio = " << ratio << std::endl;
else
    std::cout << "Could not read param2 from config file" << std::endl;

return EXIT_SUCCESS;
}

一切都很好UNTIL我取消注释第11行const std::string blah = "wtf";中的字符串初始化,或第6行中的字符串初始化。出于某些令人难以置信的原因,执行此操作会导致对{{1}的第二个函数调用返回false。我试过调试这个,但是当我在调试模式下构建项目时它工作正常。 问题仅发生在发布模式

函数getParamFromConfigFile()如下:

getParamFromConfigFile()

如果有帮助,确切的失败点就是检查template <typename T> bool getParamFromConfigFile(const std::string& config_file_path, const std::string& param_name, T& param_value) { YAML::Node node = YAML::LoadFile(config_file_path); std::vector<std::string> split_name; boost::split(split_name, param_name, boost::is_any_of("/")); std::string key_str = split_name.back(); split_name.pop_back(); for(const std::string& str: split_name) { if (node.IsNull()) { std::cout << "Node is null. This happened before dereferencing " << str << std::endl; return false; } node = node[str]; } if (!node.IsSequence()) { std::cout << "Final nested map's value is not a sequence" << std::endl; std::cout << "Is map? " << (node.IsMap() ? "YES" : "NO") << std::endl; std::cout << "Is scalar? " << (node.IsScalar() ? "YES" : "NO") << std::endl; std::cout << "Is null? " << (node.IsNull() ? "YES" : "NO") << std::endl; std::cout << "Is defined? " << (node.IsDefined() ? "YES" : "NO") << std::endl; return false; } for(YAML::Node element: node) { if (!element.IsMap()) { std::cout << "Final entry is not a map!" << std::endl; return false; } if (element.size() != 1) { std::cout << "Map has multiple entries" << std::endl; return false; } if (element.begin()->first.Type() != YAML::NodeType::Scalar) { std::cout << "Key entry has to be a scalar" << std::endl; return false; } std::string key; try { key = element.begin()->first.as<std::string>(); } catch (const YAML::TypedBadConversion<std::string>& type_conversion_exception) { std::cout << "Error converting param key into string" << std::endl; std::cout << type_conversion_exception.msg << std::endl; } try { if (key == key_str) { if (element.begin()->second.Type() != YAML::NodeType::Scalar) { std::cout << "Key has been found, but the associated value is not a scalar"; return false; } param_value = element.begin()->second.as<T>(); return true; } } catch (const YAML::TypedBadConversion<T>& type_conversion_exception) { std::cout << "Error covnerting param value into specified data type" << std::endl; std::cout << type_conversion_exception.msg << std::endl; } } std::cout << "Map key was not found in the file" << std::endl; return false; } 。这个条件为真,所以函数返回false。

为什么会发生这种情况?

0 个答案:

没有答案