Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Working with null terminated values



  • 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)



  • @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
    
    }
    


  • 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.


Log in to reply