C++。 valgrind 输出 : Syscall param open(filename) points to unaddressable byte(s)

标签 c++ memory-leaks valgrind

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

Syscall param open(filename) 指向不可寻址的字节

根据我的理解(和阅读),这意味着我正在发送一个未定义、空或已删除的内存,但我无法弄清楚为什么...

这是 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;
}

你知道是什么原因造成的吗? 提前致谢。

P.S:代码运行良好。文件被正确读取和解析。

最佳答案

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

关于C++。 valgrind 输出 : Syscall param open(filename) points to unaddressable byte(s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166477/

相关文章:

java - 如何在 Eclipse 中强制线程转储?

C++ - 内存泄漏 : where is the pointer (meta) information stored?

build - 如何在 Mac 上构建和安装 Valgrind?

c++ - 在 C++ 中强制执行内存对齐

python - 如何将python列表发送到C++应用程序等待输入?

java - Web 服务客户端中的 ThreadLocal 泄漏

c++ - 无法找到 Valgrind 检测到的内存泄漏

c++ - 为什么 valgrind 说基本的 SDL 程序正在泄漏内存?

c++ - 读取字符串 vector 时出现超出范围的运行时错误

c++ - 需要解决 Makefile 读取 Storm 数据程序的问题