-2

I am trying to make a music player using TagLib library (1.11.1) in Qt5 C++. I opened multiple files and read tags. And I encountered with two difficulties. After opening a file and reading the tags I couldn't close the file and deallocate memory occupied by the file. I used delete to free up memory and close the file. But when I try this the program crashes. Here is snip code of my program:

QString audioPath("song.mp3")
auto file = new MPEG::File(audioPath.toStdWString().c_str());

if (file->isOpen()) {
    auto tag = file->ID3v2Tag();
    if (tag) {
        //read audio tags
    }
}

delete file;
  • When I remove delete file; it works perfect but there is memory leakage special when opening multiple files.

  • If I opened a file I couldn't open it again because there is no way to close the opened file without exiting the program. And leaves me with this error:

TagLib: Could not open file song.mp3

Is there a way to solve the memory leak and close the opened file?

And when I run debugging with delete file; I see this message:

The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning: Segmentation fault

Program crashing message

1 Answers1

0

You don't need delete file by yourself. Instead of this use FileRef class. It will take care about deleting file object. Example:

TagLib::String str(audioPath.toUtf8().constData(), TagLib::String::UTF8 );
#ifdef Q_OS_WIN
TagLib::FileName fname(str.toCWString());
#else
TagLib::FileName fname(str.toCString(true));
#endif
TagLib::FileRef ref(fname, true, TagLib::AudioProperties::Accurate);
if(!ref.isNull()) {
    auto mpeg = dynamic_cast<TagLib::MPEG::File*>(ref.file());

    if(mpeg) {
        TagLib::ID3v2::Tag* tag = mpeg->ID3v2Tag();
        if(tag) {
            //your code here
        }
    }
}
Evgeny
  • 3,910
  • 2
  • 20
  • 37
  • Thanks @Evgeny, It solved all my problems. I discover in my code i have deleted manually some of the tags and that led to crashing of the program. – Natty-Pluz Apr 11 '17 at 18:45