Bug 573633 - [GTK3] Tree.setImage() leaks native memory

This change adds missing free calls to pixbuf objects created by
TreeItem.setImage() and TableItem.setImage(), preventing native memory
leaks.

According to the documentation of gtk_tree_store_set() and
gtk_list_store_set(), the call will reference the pixbuf in question.
Its therefore safe to unref it after the call, allowing GTK+ to
eventually free the memory on tree destruction.

Change-Id: Id54bedb6bbe6bed5075605ae7bbb3b69c48b00a3
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/180858
Tested-by: Platform Bot <platform-bot@eclipse.org>
Reviewed-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
index 4a47171..694a312 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
@@ -125,7 +125,9 @@
 		double sy[] = new double[1];
 		Cairo.cairo_surface_get_device_scale(surface, sx, sy);
 		if (sx[0] > 1 && sy[0] > 1){
+			long oldPixbuf = pixbuf;
 			pixbuf = GDK.gdk_pixbuf_scale_simple(pixbuf, width/(int)sx[0], height/(int)sy[0], GDK.GDK_INTERP_BILINEAR);
+			OS.g_object_unref(oldPixbuf);
 		}
 	}
 	return pixbuf;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
index 794f046..2bf4392 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
@@ -1257,6 +1257,11 @@
 	}
 	int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns [index].modelIndex;
 	GTK.gtk_list_store_set (parent.modelHandle, handle, modelIndex + Table.CELL_PIXBUF, pixbuf, -1);
+	/*
+	 * Bug 573633: gtk_list_store_set() will reference the handle. So we unref the pixbuf here,
+	 * and leave the destruction of the handle to be done later on by the GTK+ tree.
+	 */
+	OS.g_object_unref(pixbuf);
 	GTK.gtk_list_store_set (parent.modelHandle, handle, modelIndex + Table.CELL_SURFACE, surface, -1);
 	cached = true;
 	/*
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
index 2ac1bc0..1095828 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
@@ -1581,6 +1581,11 @@
 	}
 
 	GTK.gtk_tree_store_set(parent.modelHandle, handle, modelIndex + Tree.CELL_PIXBUF, pixbuf, -1);
+	/*
+	 * Bug 573633: gtk_tree_store_set() will reference the handle. So we unref the pixbuf here,
+	 * and leave the destruction of the handle to be done later on by the GTK+ tree.
+	 */
+	OS.g_object_unref(pixbuf);
 	GTK.gtk_tree_store_set(parent.modelHandle, handle, modelIndex + Tree.CELL_SURFACE, surface, -1);
 	cached = true;
 	updated = true;