jeudi 25 février 2016

Why does Valgrind give "Invalid read of size 1" error?

Valgrind is giving me an invalid read error:

==37561== Invalid read of size 1
==37561==    at 0x7E81: strlen (vg_replace_strmem.c:427)

for the following code within a class (which I think may be related to a trailing \0, but I'm not sure).

std::queue<std::string> errorLog;  ///< FIFO used to store errors

const char *Monitor::popErrorFromErrorLog() {
    if (!errorLog.empty()) {
        std::string str = errorLog.front();
        errorLog.pop();
        return str.c_str();
    } else {
        return nullptr;
    }
}

void Monitor::reportError(std::string s) {
    std::ostringstream err;
    err << "Error reported: " << s << std::endl;
    errorLog.push(err.str());
}

Any ideas what's wrong here please?

Aucun commentaire:

Enregistrer un commentaire