Reputation: 213
Downloaded taglib 1.9 from https://taglib.github.io/ and compiled with the instructions provided in the INSTALL file.
The code below crashes with error "The program has unexpectedly finished."
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <taglib/mp4file.h>
#include <taglib/mpegfile.h>
#include <taglib/fileref.h>
#include <taglib/taglib.h>
#include <taglib/tag.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TagLib::FileName fn(QString("C:/Users/Bill/Desktop/02 Am I Wrong.mp3").toStdString().c_str());
TagLib::FileRef ref(fn);
qDebug() << QString::fromStdString(std::string(ref.tag()->artist().toCString()));
return a.exec();
}
Running in debug mode show me null dereference fileref.cpp at
bool FileRef::isNull() const
{
return !d->file || !d->file->isValid();
}
Running Windows 10 insider preview with static built Qt 5.0.2
I've toiled and toiled and found no helpful result.
Upvotes: 0
Views: 1206
Reputation: 1814
The Pointer returned by QString.toStdString()..c_str()
is only valid while the QString object is still alive. In your case that's only on the line where you're creating it.
So either keep a reference to your QString or don't use QString at all.
TagLib::FileRef ref("C:/Users/Bill/Desktop/02 Am I Wrong.mp3");
qDebug() << ref.tag()->artist().toCString();
Furthermore you should add some error checks. Here's an example without using qt:
#include <taglib/fileref.h>
#include <taglib/taglib.h>
#include <taglib/tag.h>
int main(int argc, char *argv[])
{
TagLib::FileRef ref("./Falco Benz - Hat Tricks (Original Mix).mp3");
if(!ref.isNull() && ref.tag() != NULL) {
std::cout << ref.tag()->artist().toCString() << std::endl;
}
}
If I omit the error checks, and the file does not exists, I get a segfault.
Upvotes: 1