[vala] codegen: check return value before ref_sink the value



commit 5555a7cc6b9fac3395d4b18465972deb33ee0662
Author: Thijs Vermeir <thijsvermeir gmail com>
Date:   Fri Jun 8 13:46:07 2012 +0200

    codegen: check return value before ref_sink the value
    
    When a function is expected to give back a floating reference, the
    caller is trying to ref_sink the reference after calling this function.
    But when the function fails and returns NULL, the ref_sink function is
    still called. This can lead to a runtime warning as the ref_sink
    function is not expecting a NULL value. Additionally remove all
    ref_sink_function_void, as this is only for C convenience.

 codegen/valaccodeattribute.vala                    |   19 -------------------
 codegen/valaccodebasemodule.vala                   |   20 ++++----------------
 vapi/gstreamer-0.10.vapi                           |    4 ++--
 .../gstreamer-0.10/gstreamer-0.10-custom.vala      |    2 +-
 4 files changed, 7 insertions(+), 38 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index f22598b..2ea1244 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -163,24 +163,6 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
-	public bool ref_sink_function_void {
-		get {
-			if (_ref_sink_function_void == null) {
-				if (ccode != null && ccode.has_argument ("ref_sink_function_void")) {
-					_ref_sink_function_void = ccode.get_bool ("ref_sink_function_void");
-				} else {
-					var cl = (Class) sym;
-					if (cl.base_class != null) {
-						_ref_sink_function_void = CCodeBaseModule.get_ccode_ref_sink_function_void (cl.base_class);
-					} else {
-						_ref_sink_function_void = false;
-					}
-				}
-			}
-			return _ref_sink_function_void;
-		}
-	}
-
 	public string unref_function {
 		get {
 			if (!unref_function_set) {
@@ -489,7 +471,6 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string? _unref_function;
 	private bool unref_function_set;
 	private string _ref_sink_function;
-	private bool? _ref_sink_function_void;
 	private string? _copy_function;
 	private bool copy_function_set;
 	private string? _destroy_function;
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index c158eaf..55ea4cc 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3944,15 +3944,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		}
 	}
 
-	bool is_ref_sink_function_void (DataType type) {
-		var cl = type.data_type as Class;
-		if (cl != null) {
-			return get_ccode_ref_sink_function_void (cl);
-		} else {
-			return false;
-		}
-	}
-
 	bool is_free_function_address_of (DataType type) {
 		var cl = type.data_type as Class;
 		if (cl != null) {
@@ -5301,9 +5292,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			if (sink_func != "") {
 				var csink = new CCodeFunctionCall (new CCodeIdentifier (sink_func));
 				csink.add_argument (result.cvalue);
-				if (!is_ref_sink_function_void (type)) {
-					result.cvalue = csink;
-					requires_temp_value = true;
+				if (type.nullable) {
+					var is_not_null = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, result.cvalue, new CCodeIdentifier ("NULL"));
+					var csink_stat = new CCodeIfStatement (is_not_null, new CCodeExpressionStatement (csink));
+					ccode.add_statement (csink_stat);
 				} else {
 					ccode.add_expression (csink);
 				}
@@ -5824,10 +5816,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return get_ccode_attribute(cl).free_function_address_of;
 	}
 
-	public static bool get_ccode_ref_sink_function_void (Class cl) {
-		return get_ccode_attribute(cl).ref_sink_function_void;
-	}
-
 	public static string get_ccode_unref_function (ObjectTypeSymbol sym) {
 		return get_ccode_attribute(sym).unref_function;
 	}
diff --git a/vapi/gstreamer-0.10.vapi b/vapi/gstreamer-0.10.vapi
index 0c2ba87..7a57f70 100644
--- a/vapi/gstreamer-0.10.vapi
+++ b/vapi/gstreamer-0.10.vapi
@@ -1,4 +1,4 @@
-/* gstreamer-0.10.vapi generated by vapigen, do not modify. */
+/* gstreamer-0.10.vapi generated by lt-vapigen, do not modify. */
 
 [CCode (cprefix = "Gst", lower_case_cprefix = "gst_")]
 namespace Gst {
@@ -829,7 +829,7 @@ namespace Gst {
 		public void weak_ref (Gst.MiniObjectWeakNotify notify, void* data);
 		public void weak_unref (Gst.MiniObjectWeakNotify notify, void* data);
 	}
-	[CCode (cheader_filename = "gst/gst.h", ref_function = "gst_object_ref", ref_sink_function = "gst_object_ref_sink", ref_sink_function_void = true, unref_function = "gst_object_unref")]
+	[CCode (cheader_filename = "gst/gst.h", ref_function = "gst_object_ref", ref_sink_function = "gst_object_ref_sink", unref_function = "gst_object_unref")]
 	public abstract class Object : GLib.Object {
 		public Gst.ObjectFlags flags;
 		public weak GLib.Mutex @lock;
diff --git a/vapi/packages/gstreamer-0.10/gstreamer-0.10-custom.vala b/vapi/packages/gstreamer-0.10/gstreamer-0.10-custom.vala
index 430f191..faefd0d 100644
--- a/vapi/packages/gstreamer-0.10/gstreamer-0.10-custom.vala
+++ b/vapi/packages/gstreamer-0.10/gstreamer-0.10-custom.vala
@@ -127,7 +127,7 @@ namespace Gst {
 		public unowned string id_description ();
 	}
 
-	[CCode (ref_function = "gst_object_ref", unref_function = "gst_object_unref", ref_sink_function = "gst_object_ref_sink", ref_sink_function_void = true)]
+	[CCode (ref_function = "gst_object_ref", unref_function = "gst_object_unref", ref_sink_function = "gst_object_ref_sink")]
 	public abstract class Object {
 		public unowned Gst.Object @ref ();
 		public void unref ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]