[Qt-interest] Segmentation fault while converting from QString to char*

J-P Nurmi jpnurmi at gmail.com
Mon Apr 19 10:18:02 CEST 2010


On Mon, Apr 19, 2010 at 11:07 AM, Alexandre Beraud <aberaud at infflux.com> wrote:
> Hi,
>
> Where is the memory allocation for 'file_name' ? If you allocate before
> getting the pointer to ba.data() how do you know the size you need to
> allocate ?
>
> Regards,
>
> Alex
>
> prashant bhutani a écrit :
>> Hi everyone,
>>
>> I am making a program which fetch a file from directory using
>> *getOpenFileName* and it returns the path as a *QString*.
>> Now I have to convert the path in *char** which i am doing using
>>          *QByteArray ba = qt_file_name.toLatin1();    *//qt_file_name
>> is path of file in Qstring
>> *         const char* temp = ba.data();                        *
>> *         strcpy(file_name,temp);                                 *//
>> file_name is in char*
>>
>> Now the code is causing segmentation fault in my program as it runs
>> fine when these statements are made comments.
>> Can anyone please tell the reason for segmentation fault and how to
>> remove it?
>>
>> I have also used *toLatin1* ,* toAscii* to convert QSrting to char*
>> but all cause same segmentation fault.
>>
>> Regards,
>> Prashant

http://qt.nokia.com/developer/faqs/faq.2007-01-30.9032238253

"Note that it is necessary to store the bytearray before you call
data() on it, a call like the following [...] will make the
application crash as the QByteArray has not been stored and hence no
longer exist"

--
J-P Nurmi




More information about the Qt-interest-old mailing list