C++. valgrind outputs: Syscall param open(filename) points to unaddressable byte(s)

c++memory-leaksvalgrind

i'm working on a little project which amongst other things parses some files.
when i check my project with valgrind i get this error:

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

to my understanding (and reading) it means that im sending a memory that is not defined, null or was deleted but i cant figure out why…

this is engine.cpp. its constructor receives the "char** argv" variable from the console

//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;
}

do you know what is causing the problem?
thx in advance.

P.S: the code is working fine. the file is read and parsed correctly.

Best Solution

My first guess is that args[1] isn't well defined/allocated when you construct your Engine. What do you expect to have in **args? How are you calling your constructor? I'd guess something like Engine(argv) in main, but if you never checked to see if argc was larger than 1, you'd be passing an array with uninitialized memory and it'd choke when you finally tried to use it.

Related Question