c++.valgrind输出:Syscall param open(filename)指向不可修改的字节



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

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

在我的理解(和阅读)中,这意味着我发送了一个未定义、无效或已删除的内存,但我不明白为什么。。。

这是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:代码运行良好。文件被正确读取和解析。

我的第一个猜测是,在构造Engine时,args[1]没有得到很好的定义/分配。您希望**args中包含什么?你是如何调用构造函数的?我猜main中有类似Engine(argv)的内容,但如果你从未检查过argc是否大于1,那么你会传递一个内存未初始化的数组,当你最终尝试使用它时,它会阻塞。

最新更新