Skip to content

Instantly share code, notes, and snippets.

Created May 18, 2015 12:43
Show Gist options
  • Save anonymous/4233fcf7325112b961e5 to your computer and use it in GitHub Desktop.
Save anonymous/4233fcf7325112b961e5 to your computer and use it in GitHub Desktop.
Memory leak whilst reading from TextTag.foreground_rgba
using Gtk;
//compile with: valac --save-temps -g --pkg gtk+-3.0 --pkg gee-0.8 gdk_rgba.vala
void main(string[] args)
{
Gtk.init(ref args);
TextView textView;
Gtk.TextBuffer buffer;
Gtk.TextIter startIter, endIter;
Window win;
string tagName = "";
textView = new TextView();
buffer = textView.buffer;
int loopCounter = 0;
weak Gdk.RGBA tmpColor = Gdk.RGBA();
Timer timer = new Timer();
timer.start();
double time_elapsed = timer.elapsed();
while (true)
{
tagName = "tag_" + (loopCounter++).to_string();//create a unique tagname.
buffer.set_text(tagName.to_string()+"\n", -1);//write "tag_<num>" into buffer
Gtk.TextTag tag = buffer.create_tag(tagName);//create a tag named "tag_<num>"
tmpColor.parse("blue");//choose some random color
tag.foreground_rgba = tmpColor;//assign it to the yet uninitialized foreground_rgba of the texttag.
// *** The leak occours here. Skipping the following line will keep the memory usage static (== no leakage). ***
stdout.printf(tag.foreground_rgba.to_string()+"\n");//most simple access without buffering leads to memleak
//string strColor = tag.foreground_rgba.to_string();//so does buffering to a string...
//weak Gdk.RGBA tmpColor2 = tag.foreground_rgba;//..and buffering to a variable of the original type.
// ** The leak occours above ***
buffer.get_start_iter(out startIter);
buffer.get_end_iter(out endIter);
buffer.apply_tag(tag, startIter, endIter);//apply the tag from the beginning to the end of the written text.
//when done, delete everything.
Gee.ArrayList<weak TextTag> tagList = new Gee.ArrayList<weak TextTag>();
buffer.tag_table.foreach((tmpTag) => { tagList.add(tmpTag); } );//fetch all created texttags
foreach(TextTag tmpTag in tagList)
{
tmpTag.foreground_rgba.free();
buffer.tag_table.remove(tmpTag);//remove the tags from the buffer. else they'll remain even after clearing it.
}
buffer.delete(ref startIter, ref endIter);//remove all written text.
if( timer.elapsed() >= (time_elapsed+10) )//after ~10 seconds...
break;//..exit the while-loop.
}
{//we don't really want to arrive here. the loop above matters.
win = new Window();
win.destroy.connect (Gtk.main_quit);
win.title = "Gdk.RGBA leakage";
win.border_width = 5;
win.window_position = WindowPosition.CENTER;
win.set_default_size(655, 480);
win.add(textView);
win.show_all();
Gtk.main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment