here开发者_如何学C is a simple question. Can you help me find the memory leak in this Vala code ? If it helps I can post the generated c code too ^^
using GLib;
using Gtk;
using Gee;
void test1 ()
{
Gee.ArrayList<Gdk.Pixbuf> list = new Gee.ArrayList<Gdk.Pixbuf>();
for( int a = 0; a < 10000; a++)
{
string path = "/usr/share/icons/gnome/48x48/stock/data/stock_lock.png";
list.add( new Gdk.Pixbuf.from_file( path ) );
}
list.clear();
// when the function returns it *should* free all alocated memory, or am I missing something?
}
int main (string[] args)
{
Gtk.init( ref args);
// the memory usage here is ~3mb
test1();
// here it is ~94mb
Gtk.main();
return 0;
}
I've reproduced this on the latest versions of Vala (0.10.1 and 0.11.1). I've looked over the .c code valac generates and don't see an obvious problem there, although it's obvious the pixbufs are leaking (valgrind confirms this). I reported it as a bug here:
https://bugzilla.gnome.org/show_bug.cgi?id=633869
Update: The bug is closed. Investigation shows there is no memory leak per se, but (most likely) that the memory is being allocated and held when it's freed by a suballocator or some-such. As Evan pointed out, if you call the test function in a loop, the total memory size never exceeds 90MB, indicating it's not a memory leak.
精彩评论