Results 1 to 20 of 22

Thread: Issue using QString

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Issue using QString

    Fortunately this is the newbie forum because I feel dumb all of a sudden. I did a search. My problem is that calling toStdString() on a QString crashes my program resulting in an access violation

    The following code compiles and runs, but then crashes:

    Qt Code:
    1. void q_string_to_std_string()
    2. {
    3. QString herp("derp");
    4. herp.toStdString();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Any help is appreciated.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Issue using QString

    Yes, compiles, runs, does nothing (i.e. discards the result of QString::toStdString()). Your crash is somewhere else. Run your program in a debugger and see what the backtrace looks like.

  3. #3
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QString

    No, that is the whole program. I pinpointed the issue, so I wrote some trivial program and sure enough. To be more clear, this prints "hi" one time and then crashes.

    Qt Code:
    1. int main()
    2. {
    3. QString herp("derp");
    4. std::cout << "hi\n";
    5. herp.toStdString();
    6. std::cout << "hi\n";
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    To reiterate, if I comment out the one line with the call to toStdString() or replace it with toASCII() ot something, there is no crash.
    Last edited by prophetjohn; 14th September 2011 at 17:12.

  4. #4
    Join Date
    Aug 2011
    Location
    Malmö, Sweden
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue using QString

    There shouldn't be any problem with that code sample, can you post the error messanges?

    //Fredrik

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Issue using QString

    Quoting myself:
    Run your program in a debugger and see what the backtrace looks like.
    Your code runs just fine here on 32-bit Linux, Qt 4.7.2, GCC 4.4.5. Without your error message, backtrace, or any details about which OS/compiler/Qt version we can only guess at how this is broken.

    Does it behave differently if you don't ignore the return value from toStdString() ?
    Last edited by ChrisW67; 15th September 2011 at 00:59.

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Issue using QString

    If your Qt installation was configured without support for STL or other C++ template classes, this function may case problems.

    What do you mean "but then crashes"? Provide the error messages produced.

  7. #7
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QString

    Here is what is displayed in the console during the crash

    Qt Code:
    1. 'qtt.exe': Loaded 'C:\Users\Josh\Documents\Visual Studio 2008\Projects\qtt\Debug\qtt.exe', Symbols loaded.
    2. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
    3. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
    4. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll'
    5. 'qtt.exe': Loaded 'C:\Windows\system\QtCore4.dll', Binary was not built with debug information.
    6. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
    7. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
    8. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
    9. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
    10. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
    11. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
    12. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll'
    13. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
    14. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll'
    15. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll'
    16. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
    17. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll'
    18. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll'
    19. 'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvcp90.dll'
    20. 'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvcr90.dll'
    21. 'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcr90d.dll', Symbols loaded.
    22. 'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcp90d.dll', Symbols loaded.
    23. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
    24. 'qtt.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
    25. First-chance exception at 0x683631ea (msvcr90d.dll) in qtt.exe: 0xC0000005: Access violation reading location 0xcccccbf4.
    26. Unhandled exception at 0x683631ea (msvcr90d.dll) in qtt.exe: 0xC0000005: Access violation reading location 0xcccccbf4.
    To copy to clipboard, switch view to plain text mode 

    I am compiling with VC++ 2008 on Window 7 x64 Ultimate. I was initially using library version 4.4.1 which I compiled from source and suspected that may be the problem, but this is happening after I updated and am using the libs and includes from the SDK version 4.7.4

    It doesn't behave different if I capture the the output of the function. I removed the assignment suspecting and error could be happening there, but it doesn't look like that's the case.

    Also, when it crashes, VS opens the file dbgdel.cpp and points at the following line

    Qt Code:
    1. _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
    To copy to clipboard, switch view to plain text mode 

    saying that it is the next to execute.

    I'm fairly new to VS because most of my previous programming was done in command line Linux, so I'm not terribly familiar with the debugger, but if more info is needed, I will try to find it. Thanks.

    edit: clicking around, it looks like the problem occurs during a delete call, but that's as much as I can tell.
    Last edited by prophetjohn; 15th September 2011 at 05:29.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Issue using QString

    Quote Originally Posted by SixDegrees View Post
    If your Qt installation was configured without support for STL or other C++ template classes, this function may case problems.
    Surely the code would fail to compile if that were the case. QString::toStdString() would not be present after the pre-processor removed it (QT_NO_STL).

  9. #9
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QString

    I've essentially worked around this issue using only c-strings a QStrings and no std::strings, but if anyone else has ideas, I'd love to find out what my issue is. My only idea is that it has something to do with me having both Qt 4.4.1 and 4.7.3 on this machine, but I've updated all my environmental variables and lib/src/include directories

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Issue using QString

    Try building:
    Qt Code:
    1. #include <string>
    2. #include <iostream>
    3.  
    4. int main(void)
    5. {
    6. std::string s("Hello", 5); // This is what Qt does in toStdString()
    7. std::cout << s << std::endl;
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    If that fails when run then the problem has nothing to do with Qt.

  11. #11
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue using QString

    Quote Originally Posted by ChrisW67 View Post
    Try building:
    Qt Code:
    1. #include <string>
    2. #include <iostream>
    3.  
    4. int main(void)
    5. {
    6. std::string s("Hello", 5); // This is what Qt does in toStdString()
    7. std::cout << s << std::endl;
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    If that fails when run then the problem has nothing to do with Qt.
    This executes without any problem.

  12. #12
    Join Date
    May 2011
    Posts
    239
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60
    Thanks
    4
    Thanked 35 Times in 35 Posts

    Default Re: Issue using QString

    I don't see why crashing of a statement that does nothing, and makes no sense, is worth all this discussion.
    herp.toStdString(); should obviously be removed.

    Now, if this would crash, it would be worth solving, as it actually does something with the conversion:
    std::string srt = herp.toStdString();

Similar Threads

  1. Replies: 2
    Last Post: 11th August 2011, 16:42
  2. Replies: 5
    Last Post: 13th May 2011, 01:02
  3. Replies: 4
    Last Post: 1st February 2010, 15:21
  4. QString formatting issue.
    By George Neil in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2009, 10:00
  5. Replies: 4
    Last Post: 31st January 2008, 21:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.