Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Issue using QString

  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,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    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,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    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();

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue using QString

    I had some crashes with toStdString() myself. If I remember correctly adding a line of code before that line was somehow influencing the crash, that was really weird. I don't remember what I did to solve it though. I think contrary to what it looks like, they were not problems with toStdString().
    Last edited by wysota; 17th September 2011 at 09:17.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Issue using QString

    Quote Originally Posted by mvuori View Post
    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();
    This isn't some master stroke of a program I'm working on. Instead of posting 6000 lines of code for debugging, I isolated the problem and posted it on its own.

    In short, my programs crash when I call the function QString::toStdString(). This is a problem that I would like to resolve so that I can use it in code that does do something.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue using QString

    You isolated the place where the error manifests itself, not where it actually happens. I can assure you I call QString::toStdString() many times in my code and it works just fine, the problem is not with toStdString() itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Issue using QString

    That's not what I'm saying. I don't think there is an inherent problem with toStdString() itself. I know that the problem is on my end. What I'm saying is, is that suggesting to remove that line of code is not helpful. Whether I assign the return to a variable does not affect whether it executes successfully, so I removed the irrelevant details. I assumed that removing irrelevant details would be helpful.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue using QString

    If the code you posted crashes and since we know the code itself is correct and it works, then we can assume the error is elsewhere and you can remove the whole code and focus on everything else
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Issue using QString

    In short, my programs crash when I call the function QString::toStdString().
    Even something like this wont work for you ? :
    Qt Code:
    1. #include <QString>
    2. #include <string>
    3. #include <iostream>
    4.  
    5. int main(){
    6. QString str("test string");
    7. std::string s = str.toStdString();
    8. std::cout << s << std::endl;
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

  19. #19
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue using QString

    I have found the solution to this.

    If you downloaded a pre-built version of Qt, and are using VS2010, you will get this problem. Rebuild QT with VS2010 and it fixes it (I tested it).

    So, it was toStdString causing the error, because of mismatches between VS2008 and VS2010 code generation for STL/Qt. It was not some other user code before, causing it to manifest there.

    I hope you get your problem fixed

    Keep well,
    David

  20. #20
    Join Date
    May 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Issue using QString

    What causes QString::toStdString() crashing is usually compiling and linking settings, or mixing debug and release version library, or different compilers.

    If you make sure all libs have been compiled with the same compilers with the exactly compiling and linking settings, it would work fine.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.