Results 1 to 3 of 3

Thread: Conversion of QString to char array yields garbage when in debug mode

  1. #1
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Question Conversion of QString to char array yields garbage when in debug mode

    Hi,

    during debug mode (F5 with default keys) the conversion from QString to char* fails and produces garbage data. That way, I cannot debug my program properly. When running my program normally (Ctrl+R) it's working just fine.

    Below you will find a small example, that determines the type of drives in Windows. I tested with Qt 5.5.1 and Qt 5.4.2. Qt Creator is at version 3.6.1.

    Qt Code:
    1. #include <iostream>
    2. #include <windows.h>
    3. #include <winbase.h>
    4. #include <QtCore>
    5. using namespace std;
    6.  
    7. int main(void)
    8. {
    9. QFileInfoList localDrives = QDir::drives();
    10. for (QFileInfo info : localDrives)
    11. {
    12. const char *drive = QDir::toNativeSeparators(info.path()).toStdString().c_str();
    13. UINT drivetype = GetDriveTypeA(drive);
    14. qDebug() << info.path().left(1) << QString::fromLocal8Bit(drive) << "is" << drivetype;
    15. }
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    Here is the output during debug mode. Of course, GetDriveTypeA(...) returns always 1 here. Sh*t in, sh*t out.
    Qt Code:
    1. Debugging starts
    2. "C" "««««««««îþîþ\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000ma?\u0003OÇ\u0000\u0018\u0001ð\u00ADº\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000««««««««\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000la?\u0002OÇ\u0000\u001CÈ\u00ADÆk««««««««îþîþ\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000la?\u0002NÇ\u0000\u0018ȬÆk\u0001\u0000\u0000\u0000««««««««\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000ka<\u0006NÇ\u0000\u0000\u0010G€\u0000P\u0019€\u0000îþîþîþîþla?\u0002IÇ\u0000\u0018ȬÆk\u0000\u0000\u0000\u0000««««««««\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000ma?\u0003NÇ\u0000\u001C(¼Æk\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000««««««««îþîþ\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000na?\u0000OÇ\u0000$(¼Æk\u0003\u0000\u0000\u0000\u0000\u0000\u0000\u0000««««««««îþîþîþîþîþîþ\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000la?\u0002LÇ\u0000\u0018ȬÆk\u0002\u0000\u0000\u0000««««««««\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000la?\u0002NÇ\u0000\u0018ȬÆk\u0003\u0000\u0000\u00ò¥êBio
    3. "D" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    4. "E" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    5. "F" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    6. "G" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    7. "I" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    8. "P" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    9. "Q" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    10. "R" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    11. "W" "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþoa?\u0001VÇ" is 1
    12. Debugging has finished
    To copy to clipboard, switch view to plain text mode 

    And here's the output from normal run:
    Qt Code:
    1. Starting D:\Andreas\Qt\build-StringTest-Desktop_Qt_5_5_1_MinGW_32bit2-Debug\debug\StringTest.exe...
    2. "C" "C:\\" is 3
    3. "D" "D:\\" is 3
    4. "E" "E:\\" is 5
    5. "F" "F:\\" is 2
    6. "G" "G:\\" is 2
    7. "I" "I:\\" is 3
    8. "P" "P:\\" is 5
    9. "Q" "Q:\\" is 5
    10. "R" "R:\\" is 5
    11. "W" "W:\\" is 3
    12. D:\Andreas\Qt\build-StringTest-Desktop_Qt_5_5_1_MinGW_32bit2-Debug\debug\StringTest.exe exited with code 0
    To copy to clipboard, switch view to plain text mode 

    What could be the problem and can I do something to solve it?

    Merci.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Conversion of QString to char array yields garbage when in debug mode

    That this works in normal run mode is just conincidence, it is always wrong.

    "drive" becomes an invalid pointer after that line of code has executed, i.e. the std::string returned by toStdString() is a temporary object and goes out of scope right after you have accessed c_str().
    Since c_str() returns a pointer to the std::string's internal data, which is deleted when the std::string is destroyed, the pointer you have in "drive" is now invalid.

    You could just keep the std::string and call c_str() when you need it to pass to GetDriveTypeA().

    Better yet would be to use QFile::decodeName() instead of toStString() to get the local representation.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    antimatter (5th April 2016)

  4. #3
    Join Date
    Aug 2012
    Posts
    18
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Re: Conversion of QString to char array yields garbage when in debug mode

    Ah, that's why.
    Thanks for that explanation. Now I see it, too.

Similar Threads

  1. Conversion of int to char insertion into a qString
    By yahalimu in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2015, 11:32
  2. Replies: 5
    Last Post: 8th September 2011, 10:38
  3. Char* <-> QString implicit conversion
    By kingfinn in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2010, 08:26
  4. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  5. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35

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.