Qt World Summit: Register Today!

Crash on exit after opening and closing QSqlDatabase

  • Qt 4.8.3, using Qt Creator

    I have made a very basic form, and as a test, am attempting to query a text field and load the contents into a text edit widget. I have verified that I can run the program, select an option from the menu which displays a basic message box, then close the dialog and the form, with no errors.

    The problem comes in after I have connected to a database, and then closed the connection. Upon closing the program (and not before), I get a dialog showing that the program has terminated unexpectedly.

    Visual Studio debugger says there is an unhandled exception, access violation reading a location. The debugging that I try to do within Qt Creator takes me to a spot in assembly code, and below in the levels and functions, it shows:
    Level - Function - File
    0 - RtlpLowFragHeapFree - ntdll
    1 - RtlFreeHeap - ntdll
    2 - HeapFree - kernel32
    3 - free - MSVCR100
    4 - QString::free - QtCore4
    5 - QXmlStreamStringRef::~QXmlStreamStringRef - QtCore4
    6 - operator<< - QtSql4
    7 - QSqlDatabase::tables - QtSql4
    8 - QHashData::free_helper - QtCore4

    I don't know what to make of this information.

    Here's the function which, after being called and completing, will cause the program to crash on exit:

    void MainWindow::populateForm()
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    try {
    bool ok = db.open();
    if (!ok) {
    QMessageBox qmb;
    QString strMsg("Database not open!\n\n");
    // Got this far w/o crash
    catch (std::exception ex){
    QMessageBox qmb;
    QString strMsg("Caught exception: \n");
    catch (std::string ex) {
    QMessageBox qmb;
    QString strMsg("Caught exception: \n");
    catch (...) {
    QMessageBox qmb;
    QString strMsg("Caught unknown exception.");



    Apparently the try/catch stuff doesn't do anything. It hasn't caught any errors that I had (previously). Sorry to go off on a rant here, but I used Qt version 3 on a Linux system in the past and made a whole (albeit basic) program that functions great with a MySQL database and I had very few issues getting it working. Matter of fact, I never once used a debugger. If it crashed after building, I always found the error in my code.

    Since I have started trying to use Qt 4 on Windows, I have had nothing but problems with the most basic of things. Is Qt 4 just that much worse, is it Windows, or have I dropped about 40 IQ points since then? I have so little code, and what I have has been taken from what is shown in examples from Qt Assistant. I can't help but feel that it's not me, though I hope it is, because I really want to use Qt, but the amount of issues I'm having so early in the process has me close to giving up on it.

    Thanks for reading, and extra thanks in case you have any advice!

  • Can anyone even point me in a direction that might help me figure this out?

  • Bumping again. Surely someone can help out, or let me know what additional information I need to provide?

  • The try/catch stuff won't do much unless your code throws exceptions: Qt doesn't.

    The port number suggests you are actually wanting to talk to a Microsoft SQL Server not MySQL. The ODBC connection should work to either database but you will require a MySQL ODBC driver to talk to MySQL. There should be a DSN called "db" that is defined to use relevant ODBC driver.

    It's not clear if your database is opening or not?
    Does this function get called once or many times? If many then you are trashing the existing connection every time, and this may be problematic if it's in use elsewhere in the program.
    Does the program fail on exit if this function is never called?

  • Thanks very much for your interest. The program does not fail if I don't call the populateForm() function.

    You are correct, I am connecting to MS SQL server. I mentioned MySQL because that's what I'd used on the project from a few years ago, but this one is MS SQL.

    The database is opening, from what I can tell. If I put bad values in when setting up the connection, then the messagebox does appear saying that the database is not open.

    As the code is, I am able to execute the populateForm() function, it returns fine, and afterwards I can interact with the form, I can use the menu and fire another function (which simply gives a hello messagebox), and there are no problems until I close the program. That's when I get the crash.

    So, it seems to be narrowed down to something in the way I'm creating the database connection, unless it's just a bug in the Qt libraries.

    Thanks again for your interest.

  • There's nothing particularly unusual about how you open the database. At the time you leave populateForm() the database will be closed (either by failing to open or line 42). If nothing else uses this connection there really should not be any dangling references to the database. Do you use this connection elsewhere? Do you do anything to the database connection as your code exits?

    Does this crash on exit?

    #include <QCoreApplication>
    #include <QtSql>
    #include <QDebug>

    int main(int argc, char **argv)
    QCoreApplication app(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    if (db.open())
        qDebug() << "Opened";
        qDebug() << "Failed";
    return 0;


    If not, then the problem lies elsewhere.

  • Well, I get an error that there is no header file "QtSql". I did find a QtSql header file in that directory, so I changed the second include line to <QtSql/QtSql>.

    The program built with no errors or warnings, but when running it, it crashes with the following error:
    The inferior stopped because it triggered an exception.

    Stopped in thread 0 by: Exception at 0x775f2cc7, code 0xc0000005: read access violation at: 0x0, flags=0x0.

    This appears to be the same error that is produced in my code if, after opening the connection, I try to create a query on it and execute it. Perhaps there is a problem with my installation of Qt?

  • bq. Perhaps there is a problem with my installation of Qt?

    Certainly looking like it. That code builds as-posted using Qt 4.8.0 and MingW, i.e. no need to change the header. Do you have "QT += sql" in your pro file? I don't have a suitable ODBC target database to test running it though.

    Are you certain that the Qt libraries found by the running program are the same version as those you built against?
    Is your Qt ODBC plugin built with the same compiler against the same version of Qt?

  • I'm not aware of what "QT += sql" in my pro file would do, or where I would add it. I was following along on some tutorials and have not seen anything like that. I am totally new to Qt Creator.

    I have only installed one version of the libraries, and I have only run the program on the same machine I'm developing on.

    I didn't build any ODBC plugin. What I am running is just what I downloaded in the Qt Libraries installer and the Qt Creator installer. I am using the VS2010 compiler.

    I'll uninstall everything and re-install.

  • QT += sql in your pro (project) file tell qmake to generate include paths and linker commands to include the Qt SQL support in your program. A minimal pro file, test.pro, that matches the sample I posted would look like
    TEMPLATE = app
    QT += sql
    SOURCES += main.cpp

    You would build the project from a command line (with MSVC environment and Qt in PATH) thus:
    c:\test> qmake
    c:\test> nmake
    c:\test> debug\test.exe

    This is all that Qt Creator is doing on your behalf.

  • Ah, that's very good to know. Thanks very much for the information. The contents of the pro file and using the command like as you showed is much more familiar to me, having come from a Linux development environment. I am about to close everything out and begin the re-install.

  • I re-installed, added the "QT += sql" line to the pro file and tried it again, but I get the same results as before. I tried using the commands you show to build the program but I don't have Qt in my PATH, and I'm not sure what you mean by "with MSVC environmnet". I'm assuming that's something to do with Microsoft Visual C.

    I feel like I am having too many rudimentary problems, and you shouldn't be having to walk me through this. Is there not some (accurate) online guide for setting up Qt?

  • I found the guides for setting up the libraries and compiler, and in the guide, I saw the lines referencing Qt in PATH and MSVC, but that's not what shows up in my configuration. I just have a line showing "Qt 4.8.3 (4.8.3) C:\Qt\4.8.3\bin\qmake.exe" and that's all. In the guide it shows things pointing to C:\QtSDK\ but I don't have such a directory. I don't know if this is a problem or just that the default location for the libraries have changed since previous versions... any ideas?

  • When you installed VS 2010 I assume it created a entry in your Start Menu for starting a command prompt. The environment this command prompt is started with is configured with the VS compilers in the PATH and a few other things. You should be able to type "nmake /?" at the prompt an get a useful output.

    In that command prompt you need to add the path to your desired Qt's bin directory, containing qmake. If your Qt library installer put the libraries into C:\Qt\4.8.4 then you should run "C:\Qt\4.8.4\bin\qtvars.bat" (or manually add that directory to your PATH. You should then be able to type "qmake -v" and get something meaningful.

Log in to reply