PDA

View Full Version : Qstring toStdString() and Multi-threaded DLL (/MD)



Daxos
12th May 2010, 11:41
Hi,
i have this problem:
I'm working on visual studio 2005 on Windows. I have compile qt source whit the standard command "configure" whitout any parameter.
On my application i would using the function to convert a Qstring to a std:string:



QString test = "Test";
std::string converted = test.toStdString();


This code work correctly if my project configuration is Multi-threaded Debug DLL (MDd) but the problem is that my application must be an Multi-threaded DLL (/MD) application but whit this configuration this code don't work correctly: i receive a null pointer.


Can anyone help me please??

high_flyer
12th May 2010, 11:55
The problem you see has nothing to do with the code you posted as such.
It probably has to do with shared resources or/and not protected shared resources in your code.
But its hard to say with out seeing more of the relevant code.

Daxos
12th May 2010, 13:28
Ok sorry..

If you want see the same problem you can:

- create a new qt application in visual Studio 2005. In the main file add this code:


#include <QString>

...
...
QString test = "Test";
std::string converted = test.toStdString();



Now, open the project settings, go to C/C++ and then Code generation.
Here the field Runtime Library is setting to "Multi-threaded Debug DLL (/MDd)". You change this setting at "Multi-threaded DLL (/MD)".

Now set a break point in your main function, run and step by step observe the value of the variable "converted": the function toStdString() does'nt work.

The problem is that my application must be configured as Multi-threaded DLL (/MD).

Thanks, Bye!!

banita
12th May 2010, 14:31
when you set option to Multi-threaded DLL (/MD), you dont have debug information in your exe file. are you sure that there is a null pointer?

sory for me poor english

high_flyer
12th May 2010, 14:40
Try printing out to std output the following:


QString test = "Test";
std::string converted = test.toStdString();
QString test2 = QString::fromStdString(converted);
qDebug()<<test2;

Daxos
12th May 2010, 14:41
Not null pointer, i have an unhandled exception. I have a null pointer if the subsystem is setting to Console application.

high_flyer
12th May 2010, 14:47
By the way, I ran the example you gave, that is, application with Multi-threaded DLL (/MD) option, and it ran fine, the ste::string got filled correctly from the QString, and back again to QString.
Your problem is somewhere else.

EDIT: wait a minute, do you have Qt release mode? maybe you only have a debug mode Qt.. although it should complain at link time then...

Daxos
12th May 2010, 14:48
Try printing out to std output the following:





The output is: "Test"

high_flyer
12th May 2010, 14:49
The output is: "Test"

Which proves the conversion works fine.

Daxos
12th May 2010, 14:53
Ok Thanks... i try to work.

Daxos
12th May 2010, 15:09
Sorry, but if i printing:

qDebug() << test.toStdString().c_str();

my output is wrong: thera are bad character after the word Test.

Is correct?

^NyAw^
12th May 2010, 18:16
Hi,

This character is the NULL character that means that the string finish there. Search on the web about strings on C or C++ and you will see that need to be terminated with "\0" that is the NULL character

high_flyer
14th May 2010, 10:13
The null character should not be printed out.
It could be that qDebug() doesn't stop at the null char when it gets a c style pointer, but I would be surprised if that is the case.

Daxos
15th May 2010, 11:30
Oh sorry, the bad character are Before "Test".. for example: "}→qwTEST"

wysota
15th May 2010, 11:57
Could you provide a minimal compilable example reproducing the problem?