Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>ba.constantData() is returning a pointer to data which will be invalid when QstrToChar finishes executing (the 8-bit converted QByteArray), when QstrToChar completes, all you have left is free'd junk.</p> <p>What if you just did:</p> <pre><code>file = new ID3(fileName.toLocal8Bit().constData()); </code></pre> <p>in your main routine?</p> <p>Actually, you still probably need to keep your own copy of this data in your private ID3 char *, since it can go away with the destruction of these temporaries.</p> <p>Your code should be this, instead:</p> <pre><code>class ID3{ std::string fileName; std::smart_ptr&lt;TagLib::FileRef&gt; file; public: ID3(std::string); QImage* artwork(); } ID3::ID3(std::string fileNameStr) { this-&gt;fileName = fileNameStr; this-&gt;file.reset(new TagLib::FileRef(fileNameStr)); qDebug()&lt;&lt;fileNameStr; //OUTPUT 2 } QImage* ID3::artwork(){ QString str = QString::fromLocal8Bit(this-&gt;fileName); qDebug()&lt;&lt;str; //OUTPUT 3 //MORE CODES------- } std::string QstrToCppString(QString str){ QByteArray ba = str.toLocal8Bit(); qDebug()&lt;&lt;ba.constData(); //OUTPUT 1 return std::string(ba.constData()); } int main(int argc, char *argv[]){ . . . QString fileName = "C:/Qt/Qt5.0.2/Projects/taglib_test/music files/Muse_-_Madness.mp3"; file = new ID3(QstrToCppString(fileName)); QImage *image = file-&gt;artwork(); } </code></pre> <p>Notice that I've wrapped your TagLib::FileRef in a smart_ptr as well, since you are new-ing it, you'll need to manage the memory. An alternative would be to write a proper destructor for your ID3 class. You're definitely leaking these currently (unless you just didn't share your destructor code).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload