The program has unexpectedly finished
-
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
codeExplanation : 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 ^^ .
-
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. -
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 .
-
Debugger screenshots
-
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.
-
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
-
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.