Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Working with null terminated values
Forum Updated to NodeBB v4.3 + New Features

Working with null terminated values

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 297 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by
    #1

    I have some trouble with QT and null terminated strings. I need to have for an external Library the same strings on Windows. Linux and macOS. For this I use a function:

    const TCHAR* convertToFoxValue(const QString& strValue)
    {
    
    #if defined (WIN32)
        TCHAR *someVar=new TCHAR[strValue.size()+1];
        strValue.toWCharArray(someVar);
        //set last caharacter to null terminator
        someVar[strValue.size()]=L'\0';
        return reinterpret_cast<const TCHAR *>(someVar);
    #else
        QByteArray pass = strValue.toUtf8();
        return pass.constData();
    #endif
    
    }
    

    The value is used in:

    file.lpszDestinationPath = convertToFoxValue(rootPath);
    

    On Windows it works well, if I use just a QByteArray on macOS, like

    QByteArray tRootPath = rootPath.toUtf8();
    file.lpszDestinationPath = tRootPath;
    

    it works well. But using on macOS

    file.lpszDestinationPath = convertToFoxValue(rootPath);
    

    The external library throws an error. It seems that constData() is not null terminated anymore.
    Is this right?

    Any idea how to solve this on macOS (I think Linux is the same)

    KroMignonK 1 Reply Last reply
    0
    • P pixbyte

      I have some trouble with QT and null terminated strings. I need to have for an external Library the same strings on Windows. Linux and macOS. For this I use a function:

      const TCHAR* convertToFoxValue(const QString& strValue)
      {
      
      #if defined (WIN32)
          TCHAR *someVar=new TCHAR[strValue.size()+1];
          strValue.toWCharArray(someVar);
          //set last caharacter to null terminator
          someVar[strValue.size()]=L'\0';
          return reinterpret_cast<const TCHAR *>(someVar);
      #else
          QByteArray pass = strValue.toUtf8();
          return pass.constData();
      #endif
      
      }
      

      The value is used in:

      file.lpszDestinationPath = convertToFoxValue(rootPath);
      

      On Windows it works well, if I use just a QByteArray on macOS, like

      QByteArray tRootPath = rootPath.toUtf8();
      file.lpszDestinationPath = tRootPath;
      

      it works well. But using on macOS

      file.lpszDestinationPath = convertToFoxValue(rootPath);
      

      The external library throws an error. It seems that constData() is not null terminated anymore.
      Is this right?

      Any idea how to solve this on macOS (I think Linux is the same)

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @pixbyte said in Working with null terminated values:

      The external library throws an error. It seems that constData() is not null terminated anymore.
      Is this right?

      No, that is not your problem.
      Your problem is the way you are doing it!

      const TCHAR* convertToFoxValue(const QString& strValue)
      {
      
      #if defined (WIN32)
          TCHAR *someVar=new TCHAR[strValue.size()+1];
          strValue.toWCharArray(someVar);
          //set last caharacter to null terminator
          someVar[strValue.size()]=L'\0';
      
          /// ==> MEMORY LEAK HERE: where do you free the allocated buffer???
          return reinterpret_cast<const TCHAR *>(someVar);
      #else
          QByteArray pass = strValue.toUtf8();
          // ==> BAD MEMORY ACCESS: pass  is a local variable which will be destroyed on function exit!!!
          return pass.constData();
      #endif
      
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      4
      • P Offline
        P Offline
        pixbyte
        wrote on last edited by
        #3

        ok, I understand. So i changed now to

        const TCHAR* convertToFoxValue(const QString& strValue)
        {
        
        #if defined (WIN32)
            TCHAR *someVar=new TCHAR[strValue.size()+1];
            strValue.toWCharArray(someVar);
            //set last caharacter to null terminator
            someVar[strValue.size()]=L'\0';
            return reinterpret_cast<const TCHAR *>(someVar);
        #else
        	TCHAR *someVar=new TCHAR[strValue.size()+1];
            QByteArray pass = strValue.toUtf8();
        	strcpy(someVar,pass.data());
            return reinterpret_cast<const TCHAR *>(someVar);
        #endif
        
        }
        

        About your question of delete I want to use:

        const TCHAR *string = convertToFoxValue(rootPath);;
        file.lpszDestinationPath = string;
        delete [] string;
        

        I hope this is not wrong.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved