[prev in list] [next in list] [prev in thread] [next in thread] 

List:       taglib-devel
Subject:    Re: Creating FileRef object from QString
From:       Plasty Grove <plasty.grove () gmail ! com>
Date:       2009-12-05 20:37:13
Message-ID: eb2963f20912051225h5c8a8b47h116a4b00751b6dc2 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@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@kde.org
> https://mail.kde.org/mailman/listinfo/taglib-devel
>
>

[Attachment #5 (text/html)]

That makes sense, but even the following doesn&#39;t work:<br><br>QString qstr = \
&quot;/home/mydir/music/song.mp3&quot;;<br>QByteArray qbArray = qstr.toLocal8Bit();<br>const char* str = \
qbArray.constData();<br><div> TagLib::FileRef tagFile(str);<br><br>In this case, I&#39;ve got a local \
object which stores the value returned by toLocal8Bit() and which is in scope when I&#39;m using str. \
Does qbArray get destroyed whenever constData() is called on it? When I&#39;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">&lt;<a href="mailto:mpyne@purinchu.net" \
target="_blank">mpyne@purinchu.net</a>&gt;</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>

&gt; Hi,<br>
&gt;<br>
&gt; I&#39;m developing using qt and have a situation where I have to create a<br>
&gt; FileRef object from a QString object.<br>
&gt;<br>
&gt; --------------------------------------------------<br>
&gt; TagLib::FileRef tagFile(&quot;/home/mydir/music/song.mp3&quot;) //---&gt; Works fine<br>
&gt; --------------------------------------------------<br>
&gt; QString qstr = &quot;/home/mydir/music/song.mp3&quot;;<br>
&gt; const char* str = qstr.toLocal8Bit().constData();<br>
&gt; TagLib::FileRef tagFile(str); //---&gt; Doesn&#39;t work, shows as empty in<br>
&gt; debugger and segfaults when I try to access<br>
&gt; --------------------------------------------------<br>
&gt;<br>
&gt; 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&#39;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>



_______________________________________________
taglib-devel mailing list
taglib-devel@kde.org
https://mail.kde.org/mailman/listinfo/taglib-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic