Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Valgrind memory analyzer in Qt Creator finds a problem and then crashes Qt



  • I am getting a message from the Valgind memory analyzer: "32,816 bytes in 1 blocks are definitely lost..."

    The function and line number where this is occurring:

    void processDir(string dn)
    {
        DIR *dir;
        dirent *pdir;
        vector<string> fls;
        vector<string>::size_type v, sz;
        string str;
        dir = opendir(dn.c_str()); // <- problem reported here
        if (dir == NULL)
            cout << "bad directory" << endl;
        else
        {
            while ((pdir = readdir(dir)) != NULL)
            {
                str = pdir->d_name;
                if (str != "." && str != ".." && str != "README.txt")
                    fls.push_back(str);
            }
            sz = fls.size();
            if (sz > MAX_FILES)
            {
                cout << "too many files, limit is " << MAX_FILES << endl;
                return;
            }
            sort(fls.begin(), fls.end());
            for(v = 0; v < sz; v++)
                processFile(dn + "/" + fls[v]);
        }
    }
    

    If I click on the message then Qt Creator crashes with no further error message.

    Should I be using QString instead of std::string?

    I'm using Qt Creator 3.5.1 on Linux Mint 18.3.


  • Lifetime Qt Champion

    @Guerrian said in Valgrind memory analyzer in Qt Creator finds a problem and then crashes Qt:

    DIR *dir;

    I think this is the problem. Have you tried adding closedir at the end of your function?

    Regards



  • Yeah, adding "closedir(dir)" gets rid of the problem. It makes sense, thanks.


Log in to reply