Important: Please read the Qt Code of Conduct -

QSqlQuery Access violation by out of scope

  • Hello all,

    first my system:
    Win 7 x64
    QT 5.7 64bit (installed from installer)

    I want to create a table in my MySQL DB. So I "login" in my DB and want to execute a statement.

    	QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    	if (!
    		return false;
    	QSqlQuery query(db);
    	const QString cmd = "CREATE TABLE IF NOT EXISTS bla (idbla INTEGER UNIQUE PRIMARY KEY, bla VARCHAR(255))";
    	bool res = query.prepare(cmd);
    	if (res)
    		if (query.exec())
    			qDebug() << "bla created succesfully";
    			qDebug() << "Error " << "MySQl Error " << query.lastError().text();
    	} <- Access Violation (ntdll.dll), because of QSqlQuery

    The Database connection ist still alive. So why I get this error? Maybe I used the wrong libmysql.dll?
    I copied libmysql.dll(mysql-connector-c-6.1.6-winx64) and qsqlmysqld.dll to my application directory.

    Best regards

  • Lifetime Qt Champion

    @masterofeye Can you post the exact error message?
    Also there is no need to call finish()

  • Hi,
    there error message say not more then fatal error at 0x000... (ntdll.dll) in UnitTest.exe 0x000... Access Violation at read from pos 0x000....

    here the orignal text in german.
    Ausnahmefehler bei 0x000000007739F23C (ntdll.dll) in UnitTest.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x000000202B2B0F98

    The finish call was just a try ;)

  • Lifetime Qt Champion

    @masterofeye Run through debugger and see what happens on the call stack

  • Nothing what i can interpret as a failure:

  • Lifetime Qt Champion

    @masterofeye Images do not work in the forum. Please upload it to somewhere and post a link. Or, even better, just copy paste here as text.

  • Really? Because I see the Image ... fantastic ..

    ntdll.dll!RtlFreeHeap()	Unknow
    kernel32.dll!HeapFree()	Unknow
    msvcr120.dll!000007feee2669d8()	Unknow
    Qt5Sql.dll!00000000612dae72()	Unknow
    qsqlmysql.dll!000007fefa8f1e05()	Unknow
    qsqlmysql.dll!000007fefa8f1d97()	Unknow
    Qt5Sql.dll!00000000612d15e4()	Unknow
    UnitTest.exe!SQLConstructor::MySQLInitialization() Zeile 35	C++
    UnitTest.exe!main(int argc, char * * argv) Zeile 23	C++

  • Lifetime Qt Champion


    Did you check that your MySQL dlls are compiled with the same compiler as Qt ?

  • As mentioned on the top, i took the libmysql.dll from the installation here mysql-connector-c-6.1.6-winx64 and the qsqlmysqld.dll from the msvc2013_64 installation. They should be compatible or ?

  • Lifetime Qt Champion

    @masterofeye said in QSqlQuery Access violation by out of scope:

    They should be compatible or ?

    Only if libmysql.dll was built with msvc2013_64.

  • Moderators

    @masterofeye said in QSqlQuery Access violation by out of scope:

    UnitTest.exe!SQLConstructor::MySQLInitialization() Zeile 35	C++
    UnitTest.exe!main(int argc, char * * argv) Zeile 23	C++

    What do you have around line 23 in your main() and what do you have around line 35 in SQLConstructor::MySQLInitialization()? It's not clear from the code snippet how the code you posted relates to these two locations.

  • So tested some more and put my code in the main function between of the QCoreApplication creation and the exec() function. And it works! I changed the QSqlQuery variable to a pointer and dellete it before the call of the exec().

    When i put the this code now back in the class and execute the function, I get the error again.
    The class inherits form QObject and uses the Q_OBJECT macro. Do I need anything else ?

    sorry here the answer from @kshegunov question

    int main(int argc, char *argv[])
    	QCoreApplication a(argc, argv);
    	SQLConstructor sql();
    	sql.MySQLInitialization();   //line 13
    	QLocalSocket socket;
    	while (!socket.isOpen());
           RW::CORE::Unit u(&socket);
           return a.exec();
    ```in MySQLInitialization happens my code posted in the first post.

  • I found it !!! .... i linked against the Qt5Sql.lib and not against the Qt5Sqld.lib ..... GRRR
    Thx for your help !!

  • Lifetime Qt Champion

    Out of curiosity, how did that happen ?

  • @SGaist Happened to me too when I was using VS Express (which does not support the addins), you have to do the linking manually (and invoke moc and stuff in prebuild) and it's easy to get it wrong

  • Lifetime Qt Champion

    @VRonin I was thinking about something along this line. Just wanted to check whether it was that or a setup issue, an IDE quirk or something more exotic :)

  • Lifetime Qt Champion

    is VS express not excotic enough ? ;)