Creating FileRef object from QString

Plasty Grove plasty.grove at gmail.com
Sat Dec 5 21:25:13 CET 2009


That makes sense, but even the following doesn't work:

QString qstr = "/home/mydir/music/song.mp3";
QByteArray qbArray = qstr.toLocal8Bit();
const char* str = qbArray.constData();
TagLib::FileRef tagFile(str);

In this case, I've got a local object which stores the value returned by
toLocal8Bit() and which is in scope when I'm using str. Does qbArray get
destroyed whenever constData() is called on it? When I'm debugging, I can
still see str holding a value when it goes to the next step.

Thanks,
Plasty.


On Sun, Dec 6, 2009 at 1:18 AM, Michael Pyne <mpyne at purinchu.net> wrote:

> 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
>
> _______________________________________________
> taglib-devel mailing list
> taglib-devel at kde.org
> https://mail.kde.org/mailman/listinfo/taglib-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.kde.org/pipermail/taglib-devel/attachments/20091206/984bbd33/attachment.htm 


More information about the taglib-devel mailing list