0

I try to make a M3U file(playlist) but when I try to write a file my program crash without an error.

bool Playlist::savePlaylist(QString path)
{
    this->filename = path;

    QFile file(path);

    if((file.open(QIODevice::WriteOnly | QIODevice::Truncate)) == false)
    {
        return false;
    }

    QTextStream out(&file);
    if(out.status() != QTextStream::Ok)
    {
        return false;
    }

    out << "#EXTM3U" << endl;

     for(int i = 0 ; i < this->getSize() ; i++)
     {
         SongInfo si = this->songs.at(i);
         int durata = this->getLengthInSeconds(si.getAddress().toString(QUrl::None));

         out << "#EXTINF:" << durata << "," << ID3_GetArtist(&si.getTag()) << " - "
            << ID3_GetTitle(&si.getTag()) << endl;

         out << si.getAddress().toString(QUrl::None) << endl;
    }

    file.close();

    return true;
}

If I try with 188 files, 1.07 Gb size on disc, after about 100 songs the program crash. When I try to get the SongInfo at that position I get the crash. If I try with let's say 30-40 songs everything works perfect. I don't know what could be the problem. I've tried to write chunks, and delete those chunks from memory, I've used flush. I don't know what to do, where the problem could be. If I write to console those information(a for loop just for printing) everything is perfect, but when I try to write...crash.

What could be the problem?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

0

To me it looks weird how you iterate over the song list with this->getSize() and then get the SongInfo with this->songs.at(i);. this is not the same object as this->songs, and just by looking at your code it's not possible to know what getSize() does.

Would you care to try this?

int size = songs.size();

for(int i = 0 ; i < size ; i++)
{
   SongInfo si = this->songs.at(i);
   int durata = this->getLengthInSeconds(si.getAddress().toString(QUrl::None));

   out << "#EXTINF:" << durata << "," << ID3_GetArtist(&si.getTag()) << " - "
       << ID3_GetTitle(&si.getTag()) << endl;

   out << si.getAddress().toString(QUrl::None) << endl;
}

or even better, if songs is a QList:

foreach(SongInfo si, songs){
   int durata = this->getLengthInSeconds(si.getAddress().toString(QUrl::None));
   ...
}
dschulz
  • 4,666
  • 1
  • 31
  • 31