Skip to content

Commit c4bf62e

Browse files
committed
Fix Flac picture memory leak
1 parent 86567b7 commit c4bf62e

File tree

5 files changed

+113
-13
lines changed

5 files changed

+113
-13
lines changed

ext/taglib_flac/taglib_flac.i

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@
8686
SWIG_RubyRemoveTracking(properties);
8787
}
8888

89+
TagLib::List<TagLib::FLAC::Picture *> list = file->pictureList();
90+
for (TagLib::List<TagLib::FLAC::Picture *>::ConstIterator it = list.begin(); it != list.end(); it++) {
91+
TagLib::FLAC::Picture *picture = (*it);
92+
SWIG_RubyUnlinkObjects(picture);
93+
SWIG_RubyRemoveTracking(picture);
94+
}
95+
8996
SWIG_RubyUnlinkObjects(ptr);
9097
SWIG_RubyRemoveTracking(ptr);
9198

ext/taglib_flac/taglib_flac_wrap.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,13 @@ SWIGINTERN void TagLib_FLAC_File_close(TagLib::FLAC::File *self){
22102210
SWIG_RubyRemoveTracking(properties);
22112211
}
22122212

2213+
TagLib::List<TagLib::FLAC::Picture *> list = file->pictureList();
2214+
for (TagLib::List<TagLib::FLAC::Picture *>::ConstIterator it = list.begin(); it != list.end(); it++) {
2215+
TagLib::FLAC::Picture *picture = (*it);
2216+
SWIG_RubyUnlinkObjects(picture);
2217+
SWIG_RubyRemoveTracking(picture);
2218+
}
2219+
22132220
SWIG_RubyUnlinkObjects(ptr);
22142221
SWIG_RubyRemoveTracking(ptr);
22152222

test/data/flac-create.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
using namespace TagLib;
1111

1212
int main(int argc, char **argv) {
13-
if (argc != 2) {
14-
std::cout << "usage: " << argv[0] << " file" << std::endl;
13+
if (
14+
((argc < 2) || (argc > 3))
15+
||
16+
((argc == 3) && (argv[2] != "nopic"))
17+
) {
18+
std::cout << "usage: " << argv[0] << " file [nopic]" << std::endl;
1519
exit(1);
1620
}
1721
char *filename = argv[1];
@@ -39,20 +43,23 @@ int main(int argc, char **argv) {
3943
tag->addField("MULTIPLE", "A");
4044
tag->addField("MULTIPLE", "B", false);
4145

42-
FLAC::Picture *picture = new FLAC::Picture();
4346

44-
picture->setType(FLAC::Picture::FrontCover);
45-
picture->setMimeType("image/jpeg");
46-
picture->setDescription("Globe");
47-
picture->setWidth(90);
48-
picture->setHeight(90);
49-
picture->setColorDepth(8);
50-
picture->setNumColors(0);
47+
if (argc == 2) {
48+
FLAC::Picture *picture = new FLAC::Picture();
5149

52-
ByteVector data = getPictureData("globe_east_90.jpg");
53-
picture->setData(data);
50+
picture->setType(FLAC::Picture::FrontCover);
51+
picture->setMimeType("image/jpeg");
52+
picture->setDescription("Globe");
53+
picture->setWidth(90);
54+
picture->setHeight(90);
55+
picture->setColorDepth(8);
56+
picture->setNumColors(0);
5457

55-
file.addPicture(picture);
58+
ByteVector data = getPictureData("globe_east_90.jpg");
59+
picture->setData(data);
60+
61+
file.addPicture(picture);
62+
}
5663

5764
file.save();
5865
}

test/data/flac_nopic.flac

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
===============================================================================
2+
flac - Command-line FLAC encoder/decoder version 1.3.2
3+
Copyright (C) 2000-2009 Josh Coalson
4+
Copyright (C) 2011-2016 Xiph.Org Foundation
5+
6+
This program is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU General Public License
8+
as published by the Free Software Foundation; either version 2
9+
of the License, or (at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License along
17+
with this program; if not, write to the Free Software Foundation, Inc.,
18+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
===============================================================================
20+
21+
This is the short help; for all options use 'flac --help'; for even more
22+
instructions use 'flac --explain'
23+
24+
Be sure to read the list of known bugs at:
25+
http://xiph.org/flac/documentation_bugs.html
26+
27+
To encode:
28+
flac [-#] [INPUTFILE [...]]
29+
30+
-# is -0 (fastest compression) to -8 (highest compression); -5 is the default
31+
32+
To decode:
33+
flac -d [INPUTFILE [...]]
34+
35+
To test:
36+
flac -t [INPUTFILE [...]]

test/flac_picture_memory_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require File.join(File.dirname(__FILE__), 'helper')
2+
3+
class TestFlacPictureMemory < Test::Unit::TestCase
4+
5+
N = 10000
6+
7+
context "TagLib::FLAC::Picture" do
8+
9+
setup do
10+
11+
end
12+
13+
should "release memory when closing flac file with picture data" do
14+
c = 0
15+
N.times do
16+
TagLib::FLAC::File.open("test/data/flac.flac", false) do |f|
17+
f.picture_list.each do |p|
18+
x = p.data
19+
c = c + 1
20+
end
21+
end
22+
end
23+
assert_equal N,c
24+
end
25+
26+
should "process a flac file without picture data" do
27+
c = 0
28+
N.times do
29+
TagLib::FLAC::File.open("test/data/flac_nopic.flac", false) do |f|
30+
f.picture_list.each do |p|
31+
x = p.data
32+
c = c + 1
33+
end
34+
end
35+
end
36+
assert_equal 0,c
37+
end
38+
39+
teardown do
40+
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)