adviner
adviner

Reputation: 3567

Taglib Qt read access violation

It seems like when you try to get the rating from an mp3 in taglib and its been opened it will cause a read access violation:

enter image description here

Here is my code:

QFile fileInfo(file);
fileInfo.open(QIODevice::ReadOnly);
if(fileInfo.isReadable())
{
    TagLib::MPEG::File mpeg(file.toStdString().c_str());
    bool isRead = mpeg.isReadable(file.toStdString().c_str());
    if(isRead)
        rating = dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(mpeg.ID3v2Tag()->frameList("POPM").front())->rating();
}

I've tried adding a try/catch but it still fails. I have tried QLockFile with no success. Is there any way to test if a file has exclusive rights to a file or catch the read access violation?

Update Thanks to the tip I modified my code to check for invalid values first:

if(mpeg.ID3v2Tag() != 0)
{
    if(dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(mpeg.ID3v2Tag()->frameList("POPM").front()) != 0)
    {
        rating = dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(mpeg.ID3v2Tag()->frameList("POPM").front())->rating();
    }
}

Upvotes: 0

Views: 268

Answers (2)

scotchi
scotchi

Reputation: 2419

You're hitting this problem because of the code you had in this question. You're creating a POPM frame the wrong way, and then the cast is failing.

Note: dynamic_cast is a checked cast type. It returns zero when it fails, as it is here. You need to check the return value before assuming it works (otherwise you would generally use a static_cast).

Upvotes: 0

Kevin Krammer
Kevin Krammer

Reputation: 5207

Read access violation has nothing to do with file read, you program tries to do a read-access of memory it can't access.

In your case you are trying to access a null pointer (0x0).

You have two pointer access operations in your last line of code, cehck both for not being a null pointer before trying to call methods on them

Upvotes: 1

Related Questions