How can I get the size of a file when I am using recursion to look at each file?
I’m getting the next error:
project.exe exited with code -1073741819
int dir_size(const QString _wantedDirPath)
{
long int sizex = 0;
QFileInfo str_info(_wantedDirPath);
if (str_info.isDir())
{
QDir dir(_wantedDirPath);
QStringList ext_list;
dir.setFilter(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for(int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
if ((fileInfo.fileName() != ".") && (fileInfo.fileName() != ".."))
{
sizex += (fileInfo.isDir()) ? this->dir_size(fileInfo.path()) : fileInfo.size():
QApplication::processEvents();
}
}
}
return sizex;
}
Its crashing because you are recursively evaluating the same folder again and again.
The statement
sizex += this->dir_size(fileInfo.path());calls the same function recursively with the same folder name.So your stack keeps growing and eventually out of memory.
fileInfo.path()gives the same (parent) folder.fileInfo.filePath()gives the filename with the pathChange it to
sizex += this->dir_size(fileInfo.filePath());and that should fix it