Results 1 to 15 of 15

Thread: Qstring toStdString() and Multi-threaded DLL (/MD)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Qstring toStdString() and Multi-threaded DLL (/MD)

    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:

    Qt Code:
    1. QString test = "Test";
    2. std::string converted = test.toStdString();
    To copy to clipboard, switch view to plain text mode 

    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??

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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:
    Qt Code:
    1. #include <QString>
    2.  
    3. ...
    4. ...
    5. QString test = "Test";
    6. std::string converted = test.toStdString();
    To copy to clipboard, switch view to plain text mode 

    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!!

  4. #4
    Join Date
    Mar 2010
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    Try printing out to std output the following:
    Qt Code:
    1. QString test = "Test";
    2. std::string converted = test.toStdString();
    3. QString test2 = QString::fromStdString(converted);
    4. qDebug()<<test2;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    zoz (30th May 2010)

  7. #6
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    Not null pointer, i have an unhandled exception. I have a null pointer if the subsystem is setting to Console application.

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #8
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    Try printing out to std output the following:
    [CODE
    QString test = "Test";
    std::string converted = test.toStdString();
    QString test2 = QString::fromStdString(converted);
    qDebug()<<test2;
    ][/CODE]

    The output is: "Test"

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    The output is: "Test"
    Which proves the conversion works fine.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. The following user says thank you to high_flyer for this useful post:

    zoz (30th May 2010)

  12. #10
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    Ok Thanks... i try to work.

  13. #11
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    Sorry, but if i printing:

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

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

    Is correct?

  14. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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
    Òscar Llarch i Galán

  15. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qstring toStdString() and Multi-threaded DLL (/MD)

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QString::toStdString in PyQt Question
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 2nd November 2009, 20:09
  2. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  3. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15
  4. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47
  5. QString .toStdString heap bug
    By bpetty in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2006, 17:10

Tags for this Thread

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.