PDA

View Full Version : QString comparison gives wierd crash



supergillis
1st June 2009, 23:09
Hello

I get this strange crash when using toAscii() on a QString.
This is the output from the debugger:

0 QTextCodec::fromUnicode qtextcodec.cpp 1189
1 QChar::toAscii qchar.cpp 1261
2 OutputMessage::addString OutputMessage.cpp 62
3 Client::handleFirstPacket Client.cpp 87
4 Client::handleMessage Client.cpp 38
5 Client::listen Client.cpp 26
... <More>

This is the code that causes the crash:

void OutputMessage::addString(const QString& value)
{
addU16(value.length());
for(QString::const_iterator it = value.begin(); it != value.end(); it++)
addByte((*it).toAscii());
}

But I don't always get this crash. I found out that I only get it when I compare a QString with a (const char?) string.
For example: if I add this piece of code (anywhere in my application)

QString str = "test";
bool result = (str == "test");
then it causes the crash. If I remove that piece of code, then the crash doesn't occur and everything runs fine!

Help would be appreciated!
Gillis

Edit
When I use QString::localeAwareCompare instead of ==, it causes no crash. But I want to use ==, how can I do this? What am I doing wrong?