Important: Please read the Qt Code of Conduct -

QString and access violation

  • I've got what I hope is a simple question regarding an access violation error I get with QString.

    Qt Version: 4.7.3
    Platform: VS2008 on Windows 7, 64 bit
    Compilation: -debug-and-release -opensource -platform win32-msvc2008 -no-qt3support
    Compile window: VS2008 x64 Win64 Command Prompt
    Other: dual install of VS2008 and VS2010 on computer, SP/patches installed (probably not relevant, but.....)


    I simply assign a string value to a string variable associated with an object. I receive an access violation error in QString.cpp:

    @QString &QString;::operator=(const QString &other;)
    if (!d->ref.deref()) //violation occurs here
    d = other.d;
    return *this;

    The code that calls this is:

    @void SPUMain::openDb(const QString dbFileName)
    if(dbFileName.isNull() == false && dbFileName.length() > 0)
    this->dbFileName = dbFileName; // QString access violation error occurs here


    int ret = QMessageBox::information(this, "Database Open OK", "Database opened successfully");

    I've tried just about every combination I can think of, and, this error did not occurred in the past with the same application on XP.

    I'm stumped - this seems so obvious but I can't figure it out.

    I would appreciate the assistance.

    BTW - when I submitted this question, the 'Code' flag for some odd reason insists upon adding a ';' to the end of the 'other' parameter for the QString operator. Can't get it to stop - it's obviously not in the actual code.



  • Could you show the code that uses the SPUMain class?

    As for the code issue, please mention it in the bug tracker for this website or in the beta testing forum.

  • Just to be sure: your SPUMain object is actually valid, right?

  • [quote author="Andre" date="1311838423"]Just to be sure: your SPUMain object is actually valid, right?[/quote]That's why I asked :)

  • Sorry but I found the error in the code - it was so simple and it had nothing to do with QString per se.

    Can I change the subject somewhat, although it is related.

    I am using wide characters in this application. Please see code for example of copying strings to a 'standard' wide character required by a database:

    @const char_t* DAO::qStrToDbStr(const QString& qStr)
    int memLen = qStr.size() + 1;
    char_t* dbStr = new char_t[memLen];
    return (const char_t*)dbStr;

    I receive link error when I invoke the toWCharArray method.

    Note that const char_t is a typedef for wide character type wchar_t.

    I apparently have not included the correct libraries when compiling Qt since I receive the following link errors.

    Would anyone mind offering suggestions on what I'm doing wrong?


    1>DAO.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > __cdecl QString::toStdWString(void)const " (_imp?toStdWString@QString@@QEBA?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@XZ) referenced in function "public: void __cdecl DAO::openDb(class QString const &)" (?openDb@DAO@@QEAAXAEBVQString@@@Z)
    1>DAO.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __cdecl QString::toWCharArray(wchar_t *)const " (_imp?toWCharArray@QString@@QEBAHPEA_W@Z) referenced in function "public: wchar_t const * __cdecl DAO::qStrToDbStr(class QString const &)" (?qStrToDbStr@DAO@@QEAAPEB_WAEBVQString@@@Z)

  • Almost forgot: I include the following in this code....

    #include <wchar.h>

  • Visual studio projects usually have wchar_t defined as a built-in type. The Qt for VS however doesn't have this setting enabled (wchar_t is short or unsigned short on windows). Obviously short is not the same as wchar_t, which gives you linker errors. I had to tackle the same thing.

    Windows uses utf16 for wide character encoding. The following code should give you better results:

    @QString src = "whatever";
    std::wstring wstr = (const wchar_t *)src.utf16(); // windows wide encoding is utf16
    QString dst = QString::fromUtf16((const ushort *)wstr.c_str());
    // (src == dst) == true

  • Thanks Franzk for such a fast response. I'll give this a try tonight. I do prototyping for work in evenings, odd arrangement, but be that what it may I'll have to wait until this evening to try it out.

    Thank you very much for the great assistance.

    Best Regards,


  • I tried it and it works like a charm. Thanks again.....George

Log in to reply