some memory leaks

巫山霏云 freefcw at gmail.com
Thu Oct 14 14:01:06 CEST 2010


1.Valgrind log show me the memory leak like this

==5783== 166,600 (8,080 direct, 158,520 indirect) bytes in 202 blocks
are definitely lost in loss record 140 of 143
==5783==    at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
==5783==    by 0x71D2ECE:
TagLib::ASF::File::HeaderExtensionObject::parse(TagLib::ASF::File*,
unsigned int) (asffile.cpp:355)
==5783==    by 0x71D308A: TagLib::ASF::File::read(bool,
TagLib::AudioProperties::ReadStyle) (asffile.cpp:449)
==5783==    by 0x71D336C: TagLib::ASF::File::File(char const*, bool,
TagLib::AudioProperties::ReadStyle) (asffile.cpp:382)

OR

==5783== 5,264 (2,256 direct, 3,008 indirect) bytes in 47 blocks are
definitely lost in loss record 129 of 143
==5783==    at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
==5783==    by 0x71D2DCA:
TagLib::ASF::File::HeaderExtensionObject::parse(TagLib::ASF::File*,
unsigned int) (asffile.cpp:349)
==5783==    by 0x71D308A: TagLib::ASF::File::read(bool,
TagLib::AudioProperties::ReadStyle) (asffile.cpp:449)
==5783==    by 0x71D336C: TagLib::ASF::File::File(char const*, bool,
TagLib::AudioProperties::ReadStyle) (asffile.cpp:382)

So I found at asffile.cpp, in the function

TagLib::ASF::File::HeaderExtensionObject::parse(TagLib::ASF::File*,
unsigned int)

It new many objects, and append to objects, But the class
HeaderExtensionObject without a destruction function, we don't delete
it!

This is lead to memory leak, I try to delete it in the destruction
function, itis looks ok :)

class ASF::File::HeaderExtensionObject : public ASF::File::BaseObject
{
public:
 List<ASF::File::BaseObject *> objects;

 ~HeaderExtensionObject()
 {
         for(unsigned int i = 0; i < objects.size(); i++) {
               delete objects[i];
         }
 }
 ByteVector guid();
 void parse(ASF::File *file, uint size);
 ByteVector render(ASF::File *file);
};

2.The Second, valgrind show message is this:

==5783== 760 (320 direct, 440 indirect) bytes in 5 blocks are
definitely lost in loss record 119 of 143
==5783==    at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
==5783==    by 0x719E7BA:
TagLib::ID3v2::AttachedPictureFrame::AttachedPictureFrame()
(attachedpictureframe.cpp:53)
==5783==    by 0x719E88F:
TagLib::ID3v2::AttachedPictureFrameV22::AttachedPictureFrameV22(TagLib::ByteVector
const&, TagLib::ID3v2::Frame::Header*) (attachedpictureframe.cpp:211)
==5783==    by 0x71A3AD8:
TagLib::ID3v2::FrameFactory::createFrame(TagLib::ByteVector const&,
TagLib::ID3v2::Header*) const (id3v2framefactory.cpp:189)
==5783==    by 0x719B9D8: TagLib::ID3v2::Tag::parse(TagLib::ByteVector
const&) (id3v2tag.cpp:443)
==5783==    by 0x719BC63: TagLib::ID3v2::Tag::read() (id3v2tag.cpp:391)
==5783==    by 0x719C9CA: TagLib::ID3v2::Tag::Tag(TagLib::File*, long,
TagLib::ID3v2::FrameFactory const*) (id3v2tag.cpp:89)
==5783==    by 0x71931D1: TagLib::MPEG::File::read(bool,
TagLib::AudioProperties::ReadStyle) (mpegfile.cpp:401)
==5783==    by 0x7193AEF: TagLib::MPEG::File::File(char const*, bool,
TagLib::AudioProperties::ReadStyle) (mpegfile.cpp:103)

I don't know why this happened, but I found function belong:

AttachedPictureFrameV22::AttachedPictureFrameV22(const ByteVector
&data, Header *h)
{

 d = new AttachedPictureFramePrivate;

 // set v2.2 header to make fieldData work correctly
 setHeader(h, true);

 parseFields(fieldData(data));

 // now set the v2.4 header
 Frame::Header *newHeader = new Frame::Header("APIC");
 newHeader->setFrameSize(h->frameSize());
 setHeader(newHeader, false);
}

I don't know why the first setHeader is true? the Second is false??

function setHeader is this:

void Frame::setHeader(Header *h, bool deleteCurrent)
{
 if(deleteCurrent)
   delete d->header;

 d->header = h;
}

So this lead memory leak?

3.It's also a memory leak. valgrind log is this:

==5783== 455 (80 direct, 375 indirect) bytes in 5 blocks are
definitely lost in loss record 114 of 143
==5783==    at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
==5783==    by 0x71A363E:
TagLib::ID3v2::FrameFactory::createFrame(TagLib::ByteVector
const&,TagLib::ID3v2::Header*) const (id3v2framefactory.cpp:97)
==5783==    by 0x719B9D8:
TagLib::ID3v2::Tag::parse(TagLib::ByteVectorconst&) (id3v2tag.cpp:443)
==5783==    by 0x719BC63: TagLib::ID3v2::Tag::read() (id3v2tag.cpp:391)
==5783==    by 0x719C9CA: TagLib::ID3v2::Tag::Tag(TagLib::File*,
long,TagLib::ID3v2::FrameFactory const*) (id3v2tag.cpp:89)
==5783==    by 0x71931D1:
TagLib::MPEG::File::read(bool,TagLib::AudioProperties::ReadStyle)
(mpegfile.cpp:401)
==5783==    by 0x7193AEF: TagLib::MPEG::File::File(char const*,
bool,TagLib::AudioProperties::ReadStyle) (mpegfile.cpp:103)

I want know why here is memory leak, any relation the problem 2?

Thanks to all

By the way, ASF module do not deal with Unicode in wma file :(,
especially handle Chinese. So I hack it to UTF8 like this:

String
ASF::File::readString(int length)
{
 ByteVector data = readBlock(length);
 unsigned int size = data.size();
 while (size >= 2) {
   if(data[size - 1] != '\0' || data[size - 2] != '\0') {
     break;
   }
   size -= 2;
 }
 if(size != data.size()) {
   data.resize(size);
 }
 //transform Unicode to utf-8
 //author: freefcw at gmail.com
 ByteVector newdata;
 for (int i = 0; i < data.size(); i = i + 2) {
         if((data[i] < 0x80) && (data[i+1] == 0)) {
                 //cout<<"ASCII: "<<data[i]<<endl;
                 newdata.append(data[i]);
                 continue;
         } else {
                 //low byte before high byte!!
                 char ch = 0xe0 | char((data[i+1] & 0xf0) >> 4);
                 newdata.append(ch);
                 ch = (0x80 | char(((data[i+1] & 0x0F) << 2) +
((data[i] & 0xc0) >> 6)));
                 newdata.append(ch);
                 ch = 0x80 | char((data[i] & 0x3f));
                 newdata.append(ch);
         }
 }
// return in UTF-8
 return String(newdata);
}

Why ASF::File do not support firstFrameOffset?
I do it like this:

static ByteVector
dataContentGuid("\x36\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C",
16);

long ASF::File::firstFrameOffset()
{
       long position = this->find(dataContentGuid, 0);

       return position;
}


More information about the taglib-devel mailing list