Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. small console program keeps crashing for some unknown reason
Forum Updated to NodeBB v4.3 + New Features

small console program keeps crashing for some unknown reason

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreator
20 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dn588
    wrote on last edited by
    #3

    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 255

    Starting 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 -1073741819

    Debugging 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 finished

    Starting 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

    1 Reply Last reply
    0
    • D Dn588

      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;
      

      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @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);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @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
        getDuplicateList

        When 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 :)

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @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);
          
          D Offline
          D Offline
          Dn588
          wrote on last edited by
          #6

          @jsulm Thanks for taking a look. I fixed that but it's still crashing. i'll try start setting breakpoints now

          jsulmJ 1 Reply Last reply
          0
          • D Dn588

            @jsulm Thanks for taking a look. I fixed that but it's still crashing. i'll try start setting breakpoints now

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @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;
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            1
            • jsulmJ jsulm

              @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;
              
              D Offline
              D Offline
              Dn588
              wrote on last edited by
              #8

              @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 generateChecksum

              here's a link to my code if it helps to see it in QTCreator:

              https://www.sendspace.com/file/pay7cb

              jsulmJ 1 Reply Last reply
              0
              • D Dn588

                @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 generateChecksum

                here's a link to my code if it helps to see it in QTCreator:

                https://www.sendspace.com/file/pay7cb

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @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?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @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?

                  D Offline
                  D Offline
                  Dn588
                  wrote on last edited by
                  #10

                  @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

                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    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();
                    }
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      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();
                      }
                      
                      D Offline
                      D Offline
                      Dn588
                      wrote on last edited by
                      #12

                      @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?

                      mrjjM jsulmJ 3 Replies Last reply
                      0
                      • D Dn588

                        @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?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Dn588
                        Call stack

                        1 Reply Last reply
                        0
                        • D Dn588

                          @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?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Dn588 Debug your application step by step and see what happens and what is wrong.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          D 1 Reply Last reply
                          0
                          • D Dn588

                            @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?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @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.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Dn588 Debug your application step by step and see what happens and what is wrong.

                              D Offline
                              D Offline
                              Dn588
                              wrote on last edited by
                              #16

                              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...

                              mrjjM jsulmJ 2 Replies Last reply
                              0
                              • D Dn588

                                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...

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                @Dn588 said:

                                contents of a variable while debugging?

                                See picture. its on right side. showing Name / Value / type
                                and "this" variable.

                                1 Reply Last reply
                                0
                                • D Dn588

                                  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...

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by jsulm
                                  #18

                                  @Dn588 Please take a closer look at the picture @mrjj provided - in the upper right corner you will find what you're looking for.
                                  Setting only the file name is not enough if the file is not in current directory. You need to set whole path: the directory you use with QDir + file name.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  D 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @Dn588 Please take a closer look at the picture @mrjj provided - in the upper right corner you will find what you're looking for.
                                    Setting only the file name is not enough if the file is not in current directory. You need to set whole path: the directory you use with QDir + file name.

                                    D Offline
                                    D Offline
                                    Dn588
                                    wrote on last edited by
                                    #19

                                    @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...

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      Dn588
                                      wrote on last edited by
                                      #20

                                      Thanks again @mrjj and @jsulm for all help.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved