using Gtk;

//compile with: valac --save-temps -g --pkg gtk+-3.0 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;
	
	Gdk.RGBA tmpColor = Gdk.RGBA();
	
	Timer timer = new Timer();
	timer.start();
	double time_began = 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 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");//accessing the rgba-member seems to memleak
		
			//weak Gdk.RGBA tmpColor2 = tag.foreground_rgba;//..so does buffering to a variable of the original type.
		}// ** The leak occours above ***
		
		//stdout.printf( "Not accessing TextTag.foreground_rgba. " + (42).to_string() + "\n" );
		
		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.
		
		buffer.tag_table.remove(tag);//remove the tag from the buffer. clearing just the buffer will still keep created tags.
		buffer.delete(ref startIter, ref endIter);//remove all written text.
		
		if( timer.elapsed() >= (time_began+20) )//after ~20 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();
	}
}
