Creating FileRef object from QString

Michael Pyne mpyne at purinchu.net
Sat Dec 5 20:48:05 CET 2009


On Saturday 05 December 2009 13:59:35 Plasty Grove wrote:
> Hi,
> 
> I'm developing using qt and have a situation where I have to create a
> FileRef object from a QString object.
> 
> --------------------------------------------------
> TagLib::FileRef tagFile("/home/mydir/music/song.mp3") //---> Works fine
> --------------------------------------------------
> QString qstr = "/home/mydir/music/song.mp3";
> const char* str = qstr.toLocal8Bit().constData();
> TagLib::FileRef tagFile(str); //---> Doesn't work, shows as empty in
> debugger and segfaults when I try to access
> --------------------------------------------------
> 
> What am I doing wrong here?

QString::toLocal8Bit returns a QByteArray, not a const char *.
That QByteArray is destructed once .constData() returns (since there's no 
reason for C++ to keep it around).

So now your const char * pointer is dangling in the middle of freed memory, 
and you get a crash.

Regards,
 - Michael Pyne
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://mail.kde.org/pipermail/taglib-devel/attachments/20091205/c2865e63/attachment.sig 


More information about the taglib-devel mailing list