That makes sense, but even the following doesn't work:<br><br>QString qstr = "/home/mydir/music/song.mp3";<br>QByteArray qbArray = qstr.toLocal8Bit();<br>const char* str = qbArray.constData();<br><div>
TagLib::FileRef tagFile(str);<br><br>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.<br>
<br>Thanks,<br>Plasty.<br></div><br><br><div class="gmail_quote">On Sun, Dec 6, 2009 at 1:18 AM, Michael Pyne <span dir="ltr"><<a href="mailto:mpyne@purinchu.net" target="_blank">mpyne@purinchu.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div>On Saturday 05 December 2009 13:59:35 Plasty Grove wrote:<br>
> Hi,<br>
><br>
> I'm developing using qt and have a situation where I have to create a<br>
> FileRef object from a QString object.<br>
><br>
> --------------------------------------------------<br>
> TagLib::FileRef tagFile("/home/mydir/music/song.mp3") //---> Works fine<br>
> --------------------------------------------------<br>
> QString qstr = "/home/mydir/music/song.mp3";<br>
> const char* str = qstr.toLocal8Bit().constData();<br>
> TagLib::FileRef tagFile(str); //---> Doesn't work, shows as empty in<br>
> debugger and segfaults when I try to access<br>
> --------------------------------------------------<br>
><br>
> What am I doing wrong here?<br>
<br>
</div></div>QString::toLocal8Bit returns a QByteArray, not a const char *.<br>
That QByteArray is destructed once .constData() returns (since there's no<br>
reason for C++ to keep it around).<br>
<br>
So now your const char * pointer is dangling in the middle of freed memory,<br>
and you get a crash.<br>
<br>
Regards,<br>
<font color="#888888"> - Michael Pyne<br>
</font><br>_______________________________________________<br>
taglib-devel mailing list<br>
<a href="mailto:taglib-devel@kde.org" target="_blank">taglib-devel@kde.org</a><br>
<a href="https://mail.kde.org/mailman/listinfo/taglib-devel" target="_blank">https://mail.kde.org/mailman/listinfo/taglib-devel</a><br>
<br></blockquote></div><br>