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. The program has unexpectedly finished
QtWS25 Last Chance

The program has unexpectedly finished

Scheduled Pinned Locked Moved Solved General and Desktop
segfaultunexpectedly fi
9 Posts 3 Posters 4.4k Views
  • 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.
  • QjayQ Offline
    QjayQ Offline
    Qjay
    wrote on last edited by
    #1

    Hey guys . I am getting segmentation fault . I am not able to find out the reason why it is happening .

    below is my main.cpp code

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QObject>
    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlDriver>
    #include <QDebug>
    #include <QQmlEngine>
    #include <QQmlContext>
    #include <dbmanager.h>
    #include <QtWebEngine/QtWebEngine>
    
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QtWebEngine::initialize();
    
        QQmlApplicationEngine engine;
       // engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        QScopedPointer <dbmanager> dbm(new dbmanager);
        engine.rootContext()->setContextProperty("dbm",dbm.data());
    
        qmlRegisterType<dbmanager>("en.wtl.org", 1, 0, "dbmanager");
        dbmanager dbman ;
        QQmlApplicationEngine engin;
        engin.rootContext()->setContextProperty( "dbman", &dbman );
    
    
            engin.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        QDir databasePath;
    
    
        QString path = databasePath.currentPath()+"WTL.db";
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
        db.setDatabaseName(path);
        if(!db.open())
        {
            qDebug() <<"error";
        }
        else
        {
            qDebug() <<"connected" ;
        }
        QSqlQuery query;
    
       if( query.exec("CREATE TABLE IF NOT EXISTS `Pages`"
        "(  `page_ID` INTEGER NOT NULL PRIMARY KEY ,"
        "`page_revision` INT  NOT NULL);"))
       {
           qDebug() << "pages table created";
       }
       else
       {
        qDebug() <<query.lastError();
       }
    
      if(query.exec("CREATE TABLE IF NOT EXISTS `Dependencies` ("
         "`depe_ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
        " `depe_fileName` VARCHAR(45) NOT NULL,"
        "`revision_number` INTEGER NOT NULL);"))
      {
          qDebug() << "Dependencies table created";
    
      }
      else
      {
       qDebug() <<query.lastError();
      }
    
       if( query.exec("CREATE TABLE IF NOT EXISTS `Pages_has_Dependencies`"
                   "(`page_ID`	INTEGER NOT NULL,"
                  " `depe_ID`	INTEGER NOT NULL,"
                   "PRIMARY KEY(page_ID,depe_ID),"
                   "INDEX `fk_Pages_has_Dependencies_Dependencies1_idx` (`depe_ID` ASC),"
                   "INDEX `fk_Pages_has_Dependencies_Pages_idx` (`page_ID` ASC),"
                   "CONSTRAINT `fk_Pages_has_Dependencies_Pages`"
                   "FOREIGN KEY (`page_ID`)"
                   "REFERENCES `Pages` (`page_ID`)"
                   "ON DELETE NO ACTION"
                   "ON UPDATE NO ACTION,"
                   "CONSTRAINT `fk_Pages_has_Dependencies_Dependencies1`"
                   "FOREIGN KEY (`depe_ID`)"
                   "REFERENCES `Dependencies` (`depe_ID`)"
                   "ON DELETE NO ACTION"
                   "ON UPDATE NO ACTION);"))
       {
           qDebug() << "Pages_has_Dependencies table created";
       }
       else
       {
        qDebug() <<query.lastError();
       }
    
       query.clear();
       db.close();
    
       QString styling = "<style type=\"text/css\"> "
    
                 " p{"
    
                    "  display: block;"
                 " -webkit-margin-before: 1em; "
                 " -webkit-margin-after: 1em; "
                 " -webkit-margin-start: 0px; "
                 " -webkit-margin-end: 0px; "
    
                 " } "
    
    
               " body { "
              "    font-family: 'Source Sans Pro', sans-serif; "
               "   } "
    
           "   pre { "
            "      display: block; "
             "     padding: 9.5px; "
             "     margin: 0 0 10px; "
              "    font-size: 13px; "
               "   line-height: 1.42857143; "
               "   word-break: break-all; "
               "   word-wrap: break-word; "
                "  color: #333; "
              "    background-color: #f5f5f5; "
               "   border: 1px solid #ccc; "
               "   border-radius: 4px; "
           "   } "
    
    
    
            "  </style> "
               "";
    
    
       QString filename = "main.css";
    
      QFile file(filename);
          if (file.open(QIODevice::ReadWrite)) {
              QTextStream stream(&file);
              stream << styling << endl;
          }
    
    
    
    
        return app.exec();
    }
    
    

    my dbmanager.h and dbmanager.cpp code is here

    (the code is too big to paste here , as it will create mess )

    link to my dbmanager.h and dbmanager.cpp
    code

    Explanation : when i click on save page button , program executes normally till download image function comes to play .

    I think the segfault occur somewhere here :

    dbmanager *d = new dbmanager(0) ;
        d->doDownload(down_links);
    

    Look in the code ^^ .

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

      Hi
      You should put a break point in the "image function "
      and then single step until u crash so you know what line that does it.

      1 Reply Last reply
      0
      • QjayQ Offline
        QjayQ Offline
        Qjay
        wrote on last edited by Qjay
        #3

        Hey @mrjj i don't know how to set break points ( i only know about ollyDBG and i am not in windows : i know Qt has debugger too but i don't know how to set break points in it )

        Anyway i tried this approach , i placed many qDebug() <<" some text" ; at various places in codes .

        Last qDebug() message i got was before this

        dbmanager down ;
            down.doDownload(down_links);
        

        after this code there is return true statement that i get back .

        So i guess it is here where is the fault .

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

          @Qjay said:

          dbmanager down ;

          is there any change this goes out of scope ?

          1 Reply Last reply
          0
          • QjayQ Offline
            QjayQ Offline
            Qjay
            wrote on last edited by
            #5

            Debugger screenshots

            https://s31.postimg.org/fqibfx697/image.png

            https://s31.postimg.org/frn7bxbyj/image.png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Taking a quick look at your code there are several things that are fishy. First why do you have several engines and dbmanagers ?

              One thing I'm suspecting is that you have a double delete. You put one dbmanager in a QScopedPointer and then you give it to your engine. Since the dbmanager doesn't have any parent, it will be managed by the engine thus get deleted by it and by the QScopedPointer when the application ends.

              Also, the code from dbmanager contains several memory leaks. I'd recommend cleaning it up before going further.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • QjayQ Offline
                QjayQ Offline
                Qjay
                wrote on last edited by
                #7

                Hey thank you all !! for helping me so much !! It's fixed now

                ok where i messed up :

                static auto i = 0;
                       return QString("%1: %2").arg(++i).arg(p_url);
                

                in my QString add function i was not returning any value .

                another thing @SGaist pointed out about QScopePointer , yes i messed up there ( making 2 QApplication engine )

                here is my updated main.cpp code now

                #include <QGuiApplication>
                #include <QQmlApplicationEngine>
                #include <QtQml>
                #include <QObject>
                #include <QtSql>
                #include <QSqlDatabase>
                #include <QSqlDriver>
                #include <QDebug>
                #include <QQmlEngine>
                #include <QQmlContext>
                #include <dbmanager.h>
                #include <QtWebEngine/QtWebEngine>
                
                
                int main(int argc, char *argv[])
                {
                    QGuiApplication app(argc, argv);
                    QtWebEngine::initialize();
                
                    qmlRegisterType<dbmanager>("en.wtl.org", 1, 0, "dbmanager");
                        dbmanager dbman;
                        QQmlApplicationEngine engine;
                        engine.rootContext()->setContextProperty( "dbman", &dbman );
                        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                
                   // QQmlApplicationEngine engine;
                   // engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                
                   // QScopedPointer <dbmanager> dbm(new dbmanager);
                   // engine.rootContext()->setContextProperty("dbm",dbm.data());
                
                
                
                    QDir databasePath;
                
                
                    QString path = databasePath.currentPath()+"WTL.db";
                    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                    db.setDatabaseName(path);
                    if(!db.open())
                    {
                        qDebug() <<"error";
                    }
                    else
                    {
                        qDebug() <<"connected" ;
                    }
                    QSqlQuery query;
                
                   if( query.exec("CREATE TABLE IF NOT EXISTS `Pages`"
                    "(  `page_ID` INTEGER NOT NULL PRIMARY KEY ,"
                    "`page_revision` INT  NOT NULL);"))
                   {
                       qDebug() << "pages table created";
                   }
                   else
                   {
                    qDebug() <<query.lastError();
                   }
                
                  if(query.exec("CREATE TABLE IF NOT EXISTS `Dependencies` ("
                     "`depe_ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
                    " `depe_fileName` VARCHAR(45) NOT NULL,"
                    "`revision_number` INTEGER NOT NULL);"))
                  {
                      qDebug() << "Dependencies table created";
                
                  }
                  else
                  {
                   qDebug() <<query.lastError();
                  }
                
                   if( query.exec("CREATE TABLE IF NOT EXISTS `Pages_has_Dependencies`"
                               "(`page_ID`	INTEGER NOT NULL,"
                              " `depe_ID`	INTEGER NOT NULL,"
                               "PRIMARY KEY(page_ID,depe_ID),"
                               "INDEX `fk_Pages_has_Dependencies_Dependencies1_idx` (`depe_ID` ASC),"
                               "INDEX `fk_Pages_has_Dependencies_Pages_idx` (`page_ID` ASC),"
                               "CONSTRAINT `fk_Pages_has_Dependencies_Pages`"
                               "FOREIGN KEY (`page_ID`)"
                               "REFERENCES `Pages` (`page_ID`)"
                               "ON DELETE NO ACTION"
                               "ON UPDATE NO ACTION,"
                               "CONSTRAINT `fk_Pages_has_Dependencies_Dependencies1`"
                               "FOREIGN KEY (`depe_ID`)"
                               "REFERENCES `Dependencies` (`depe_ID`)"
                               "ON DELETE NO ACTION"
                               "ON UPDATE NO ACTION);"))
                   {
                       qDebug() << "Pages_has_Dependencies table created";
                   }
                   else
                   {
                    qDebug() <<query.lastError();
                   }
                
                   query.clear();
                   db.close();
                
                   QString styling = "<style type=\"text/css\"> "
                
                             " p{"
                
                                "  display: block;"
                             " -webkit-margin-before: 1em; "
                             " -webkit-margin-after: 1em; "
                             " -webkit-margin-start: 0px; "
                             " -webkit-margin-end: 0px; "
                
                             " } "
                
                
                           " body { "
                          "    font-family: 'Source Sans Pro', sans-serif; "
                           "   } "
                
                       "   pre { "
                        "      display: block; "
                         "     padding: 9.5px; "
                         "     margin: 0 0 10px; "
                          "    font-size: 13px; "
                           "   line-height: 1.42857143; "
                           "   word-break: break-all; "
                           "   word-wrap: break-word; "
                            "  color: #333; "
                          "    background-color: #f5f5f5; "
                           "   border: 1px solid #ccc; "
                           "   border-radius: 4px; "
                       "   } "
                
                
                
                        "  </style> "
                           "";
                
                
                   QString filename = "main.css";
                
                  QFile file(filename);
                      if (file.open(QIODevice::ReadWrite)) {
                          QTextStream stream(&file);
                          stream << styling << endl;
                      }
                
                
                
                
                    return app.exec();
                }
                
                

                1 more thing how to find memory leaks ? i want to improve this application

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  For example you are recreating a QNetworkAccessManager each time you do a download. The fact that you give it a parent just means it's going to get destroyed when the parent class gets destroyed. In between you are just eating up memory. Make it a class member.

                  You also re-create m_file each time and you never delete it. From your use of it, there's no need to keep as class member. Just allocate the QFile object on the stack in the function.

                  Furthermore, you have a bunch of static variables that you don't really use like down_links since you shadow it in your function with a variable of the same name.

                  dbmanager is likely a good candidate for cleanup and refactoring.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • QjayQ Offline
                    QjayQ Offline
                    Qjay
                    wrote on last edited by
                    #9

                    Thanks @SGaist !! . I will work on to improve it !!

                    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