C ++。 valgrind输出:Syscall param open(filename)指向不可寻址的字节

时间:2013-11-23 18:57:15

标签: c++ memory-leaks valgrind

我正在开展一个小项目,其中包括解析一些文件。 当我用valgrind检查我的项目时,我得到了这个错误:

Syscall param open(filename) points to unaddressable byte(s)

对我的理解(和阅读)它意味着我发送一个未定义的内存,null或被删除但我无法弄清楚为什么......

这是engine.cpp。它的构造函数接收" char ** argv"来自控制台的变量

//alot of includes and using namespace std.

Engine::Engine(char** args) {
    processConfFile(args[1]);
}

void Engine::processConfFile ( char* fileName) {
    string* fileContent = fileToString(fileName); //this line is specified at the stacktrace
    stringstream contentstream(*fileContent);
// parsing and stuff
    delete fileContent;
}

string* Engine::fileToString(const char* fileName) const{
    string* content = new string();
    ifstream ifs (fileName); // this line produces the error
    if (ifs) {
        content->assign((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
        ifs.close();
    }
    else {
        //TODO logger error.
    }
    return content;
}
你知道造成这个问题的原因吗? thx提前。

P.S:代码工作正常。正确读取和解析文件。

1 个答案:

答案 0 :(得分:1)

我的第一个猜测是,当你构建Engine时,没有很好地定义/分配args [1]。你对** args有什么期望?你怎么称呼你的构造函数?我想在main中有类似Engine(argv)的东西,但是如果你从未检查过argc是否大于1,那么你将传递一个带有未初始化内存的数组,当你最终尝试使用它时它会窒息