Solved small console program keeps crashing for some unknown reason
-
HI all
I have to write a console program for a developer internship interview before next monday and i'm hopelessly stuck... I have to write a program that finds all duplicate files in a directory and then writes a log file containing all the duplicates... When I run my program and enter the directory the program crashes and I can choose debug or close program. Any idea hwat could be causing this?
What i've done:
In main.cpp I read in a directory path from stdin and if the directory exists pass the directory on to the processDirectory function of my fileChecker class. In the fileChecker class I get the directory's entryList and then compare every file in the entryList to every other file and if two have the same size I generate a MD5 hash for both and add it to a QHash. I then have a getDuplicateList function in my fileChecker class which itterates over the QHash of possible duplicates and adds all duplicates of one file to a QString which is then added to a QStringList.My Code:
main.cpp:#include "filechecker.h"
#include <QDir>
#include <QTextStream>
#include <QString>
#include <QStringList>QTextStream in(stdin);
QTextStream out(stdout);int main() {
QDir dir;
FileChecker checker;
QString dirPath;
QStringList duplicateList;out << "Please enter directory path NOTE: use / as directory separator regardless of operating system" << endl; dirPath = in.readLine(); dir.setPath(dirPath); if(dir.exists()) { checker.processDirectory(dir); duplicateList = checker.getDuplicateList(); } else if(!(dir.exists())) out << "Directory does not exist" << endl; foreach(QString str, duplicateList){ out << str << endl; } return 0;
}
fileChecker.h:
#ifndef FILECHECKER_H
#define FILECHECKER_H
#include <QString>
#include <QByteArray>
#include <QHash>
#include <QCryptographicHash>
#include <QStringList>
#include <QDir>class FileChecker
{
public:
FileChecker();
void processDirectory(QDir dir);
QByteArray generateChecksum(QFile* file);
QStringList getDuplicateList();
private:
QByteArray generateChecksum(QString fileName);
QHash<QString, QByteArray> m_hash;
};#endif // FILECHECKER_H
fileChecker.cpp:
#include "filechecker.h"
FileChecker::FileChecker() {
}void FileChecker::processDirectory(QDir dir) {
dir.setFilter(QDir::Files); QStringList fileList = dir.entryList(); for (int i = 0; i < fileList.size(); i++) { bool possibleDuplicatesFound = false; QString testName = fileList.at((i)); QFile* testFile; testFile->setFileName(testName); foreach(QString s, fileList) { QFile* possibleDuplicate; possibleDuplicate->setFileName(s); if(testFile->size() == possibleDuplicate->size() && testFile->fileName() != possibleDuplicate->fileName()) { QByteArray md5HashPd = generateChecksum(possibleDuplicate); m_hash.insert(possibleDuplicate->fileName(), md5HashPd); possibleDuplicatesFound = true; fileList.replaceInStrings(possibleDuplicate->fileName(), ""); } QByteArray md5Hasht = generateChecksum(testFile); fileList.replaceInStrings(testFile->fileName(), ""); possibleDuplicatesFound = false; } }
}
QByteArray FileChecker::generateChecksum(QFile* file) {
if(file->open(QIODevice::ReadOnly)) { QCryptographicHash cHash(QCryptographicHash::Md5); cHash.addData(file->readAll()); QByteArray checksum = cHash.result(); return checksum; }
}
QStringList FileChecker::getDuplicateList() {
QStringList tempList;
QString tempStr;
QString currentKey;
QByteArray currentValue;
QMutableHashIterator<QString, QByteArray> i(m_hash);
do {
while (i.hasNext()){
i.next();
currentKey = i.key();
currentValue = i.value();
tempStr.append("%1 ").arg(currentKey);if (i.value() == currentValue) { tempStr.append("and %1").arg(i.key()); i.remove(); } tempList.append(tempStr); tempStr.clear(); } } while (m_hash.size() > 0); return tempList;
}
-
Hi
Using debug+single step should show you exact line ?One thing , i did wonder:
QStringList FileChecker::getDuplicateList() {
...
tempStr.append("%1 ").arg(currentKey);
i.remove(); <<<<< remove
if (i.value() == currentValue) { << and then look at value?
'
is that ok? -
Hi Thanks yes I removed that unwanted remove(). Thanks for taking a look. I can't find debug + single step?
My output when I run is:Starting C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe...
1054) CheckProcessInRegistry: Got include list but no match was found.
1054) IsProcessAllowed(C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe) (80004005)
1054) DllMain(ProcDetach) [OK]
1054) DSound_Unhook
1054) MCIWave_Unhook
1054) AudioClient_Unhook
1054) CAudioStreamMgr::Shutdown
C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe exited with code 255Starting C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe...
1F8C) CheckProcessInRegistry: Got include list but no match was found.
1F8C) IsProcessAllowed(C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe) (80004005)
1F8C) DllMain(ProcDetach) [OK]
1F8C) DSound_Unhook
1F8C) MCIWave_Unhook
1F8C) AudioClient_Unhook
1F8C) CAudioStreamMgr::Shutdown
C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe exited with code -1073741819Debugging starts
1BA0) CheckProcessInRegistry: Got include list but no match was found.
1BA0) IsProcessAllowed(C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe) (80004005)
1BA0) DllMain(ProcDetach) [OK]
1BA0) DSound_Unhook
1BA0) MCIWave_Unhook
1BA0) AudioClient_Unhook
1BA0) CAudioStreamMgr::Shutdown
Debugging has finishedStarting C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe...
1894) CheckProcessInRegistry: Got include list but no match was found.
1894) IsProcessAllowed(C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe) (80004005)
1894) DllMain(ProcDetach) [OK]
1894) DSound_Unhook
1894) MCIWave_Unhook
1894) AudioClient_Unhook
1894) CAudioStreamMgr::Shutdown
C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-duplicateFinder-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\duplicateFinder.exe exited with code 0 -
@Dn588 You never create an instance of QDir!
QDir* dir; ... dir->setPath(dirPath); // dir is not pointing to a valid QDir instance!
There is no need to use a pointer here, just put dir on the stack:
QDir dir; ... dir.setPath(dirPath);
-
@Dn588 said:
Hi
The debug is right under the normal run. ( has little bug on it)you then set a break point in a function say
getDuplicateListWhen it then stops there, you can single step with F11.
That can be really useful to find exact line even of crash, next time -
as i think @jsulm nailed this one :) -
@jsulm Thanks for taking a look. I fixed that but it's still crashing. i'll try start setting breakpoints now
-
@Dn588 Next one:
QFile* testFile; testFile->setFileName(testName);
Again: why do you use pointers where no pointers are needed?
If you want to use pointers then you have to create an instance and delete it when not needed any more:QFile* testFile = new QFile(); testFile->setFileName(testName); ... delete testFile;
-
@jsulm Thanks I see now that it crashes on line 189 of Qatomic_x86.cpp. What does this mean? I also see that the processDirectory and getChecksum functions has no issue however this crash happens before getDuplicateList runs?
the reason I use pointers is that I get an error message when trying to pass a QFile as argument to generateChecksumhere's a link to my code if it helps to see it in QTCreator:
-
@Dn588 What error do you get if you pass QFile to generateChecksum?
It should be like this:QByteArray FileChecker::generateChecksum(QFile& file) // Change QFile* to QFile& { ... }
This are C++ basics.
And again if you want to use pointer then you have to create an instance using new and later delete it. But in your case there is really no need for pointers - just fix your interface to pass QFile as reference. And even if you want to pass a pointer you can do so:QFile file; ... generateChecksum(&file);
I'm quite sure the problem is not in QAtomic. You should take a look at the call stack who is calling QAtomic?
At which line in your code does it crash? -
@jsulm I put a breakpoint at the end of generateChecksum and the program gets there with no issues so i'm guessing the processDirectory and generateChecksum functions are ok? So the crash happens after generateChecksum but i'm not exactly sure where. Where can I view the calling stack?
I get the following error when debugging
"The inferior stopped because it received a signal from the operating system
signal name: SIGSEGV
signal meaning: segmentation fault
"Yes sorry I should have passed by reference from the start. Seems like 6 month's break from C++ made me forget quite a lot...
Thanks again for all your help/patience
-
Fix like this to not to crash:
QByteArray FileChecker::generateChecksum(QFile* file) { if(file->open(QIODevice::ReadOnly)) { QCryptographicHash cHash(QCryptographicHash::Md5); cHash.addData(file->readAll()); QByteArray checksum = cHash.result(); return checksum; } // This line was missing, so you did not return a valid QByteArray instance if the if condition was false! return QByteArray(); }
-
@jsulm Thanks that worked!. However now it seems like the file i'm passing to generateChecksum has no fileName? I get 12 errors in the terminal saying " QFSFileEngine::open no file name specified". the directory I am entering has 4 .dat files which consists of 2 groups of 2 duplicate files. Any idea what is happening here? Also where can I view the calling stack in future if I have a crash like before?
-
@Dn588
Call stack -
@Dn588 Debug your application step by step and see what happens and what is wrong.
-
@Dn588 The problem is: you pass only the file name without the directory. So your application tries to open that file in current directory. You can see it easily if you debug.
-
Thanks @jsulm Will do:) is therre a way to view the contents of a variable while debugging? I google'd that error and it seems like it happens when the QFile is initialized with a name so I used QFile->setFileName(fileList.at(i)) but still no luck... Sorry for all the excessive questions... i'm writing two exams tomorrow and on mon and have this program to finish before mon to get an internship interview so stressing a bit...
-
@Dn588 said:
contents of a variable while debugging?
See picture. its on right side. showing Name / Value / type
and "this" variable. -
-
@jsulm Sorry I see that's possible in the call stack. Sorry I have an eye problem and as a result only have 4% vision so missed it the first time...
-