PDA

View Full Version : Issue using QString



prophetjohn
14th September 2011, 05:28
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:



void q_string_to_std_string()
{
QString herp("derp");
herp.toStdString();
}


Any help is appreciated.

ChrisW67
14th September 2011, 06:48
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.

prophetjohn
14th September 2011, 16:05
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.



int main()
{
QString herp("derp");
std::cout << "hi\n";
herp.toStdString();
std::cout << "hi\n";
return 0;
}


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.

fpersson
14th September 2011, 23:06
There shouldn't be any problem with that code sample, can you post the error messanges?

//Fredrik

ChrisW67
14th September 2011, 23:51
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() ?

SixDegrees
15th September 2011, 00:27
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.

prophetjohn
15th September 2011, 04:24
Here is what is displayed in the console during the crash



'qtt.exe': Loaded 'C:\Users\Josh\Documents\Visual Studio 2008\Projects\qtt\Debug\qtt.exe', Symbols loaded.
'qtt.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll'
'qtt.exe': Loaded 'C:\Windows\system\QtCore4.dll', Binary was not built with debug information.
'qtt.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll'
'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9 a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvc p90.dll'
'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9 a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvc r90.dll'
'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc 8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\ms vcr90d.dll', Symbols loaded.
'qtt.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc 8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\ms vcp90d.dll', Symbols loaded.
'qtt.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
'qtt.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
First-chance exception at 0x683631ea (msvcr90d.dll) in qtt.exe: 0xC0000005: Access violation reading location 0xcccccbf4.
Unhandled exception at 0x683631ea (msvcr90d.dll) in qtt.exe: 0xC0000005: Access violation reading location 0xcccccbf4.


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



_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));


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.

ChrisW67
15th September 2011, 22:54
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).

prophetjohn
16th September 2011, 00:40
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

ChrisW67
16th September 2011, 02:10
Try building:


#include <string>
#include <iostream>

int main(void)
{
std::string s("Hello", 5); // This is what Qt does in toStdString()
std::cout << s << std::endl;
return 0;
}

If that fails when run then the problem has nothing to do with Qt.

prophetjohn
17th September 2011, 01:51
Try building:


#include <string>
#include <iostream>

int main(void)
{
std::string s("Hello", 5); // This is what Qt does in toStdString()
std::cout << s << std::endl;
return 0;
}

If that fails when run then the problem has nothing to do with Qt.

This executes without any problem.

mvuori
17th September 2011, 07:21
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();

wysota
17th September 2011, 08:17
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().

prophetjohn
18th September 2011, 05:16
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.

wysota
19th September 2011, 15:16
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.

prophetjohn
20th September 2011, 00:34
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.

wysota
20th September 2011, 00:45
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 :)

stampede
20th September 2011, 00:48
In short, my programs crash when I call the function QString::toStdString().
Even something like this wont work for you ? :


#include <QString>
#include <string>
#include <iostream>

int main(){
QString str("test string");
std::string s = str.toStdString();
std::cout << s << std::endl;
return 0;
}

South African
13th December 2011, 20:29
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

ijab
1st June 2012, 05:26
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.

rawalinr
28th June 2017, 17:53
Thanks for this! I thought I was going mad!


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

d_stranz
28th June 2017, 18:40
The technical reason is that in VS2008 and VS2010, new applications used the compiler flag "Treat wchar_t as a built in type" with the value "No" ("/Zc:wchar_t-"), whereas the Qt distribution was built with the opposite setting ("/Zc:wchar_t"). The compiler didn't know anything was wrong, nor did the linker. It wasn't until runtime that crashes would occur.

The workaround for VS2010 was to either rebuild the Qt binaries as suggested, or to change the project settings to reverse the flag. Changing the project settings also means that any other external libraries that use wchar_t also have to be built the same way.

In current releases of Visual Studio (and Qt pre-built binaries), the default setting is now "Yes" ("/Zc:wchar_t") everywhere so everything is compatible.