[vala] codegen: Discourage copying delegates with target



commit 9f1f6cac389f4c214870720cd09050b0f68ee82a
Author: Jürg Billeter <j bitron ch>
Date:   Wed May 18 22:47:25 2011 +0200

    codegen: Discourage copying delegates with target
    
    Target destroy notify cannot be copied, which means that memory
    management will often not work as expected. Use ownership transfer,
    unowned target variable, or extra closure instead.

 codegen/valaccodebasemodule.vala  |    4 ++++
 tests/asynchronous/bug597294.vala |    2 +-
 tests/asynchronous/bug639591.vala |    2 +-
 tests/delegates/bug595639.vala    |    4 ++--
 tests/delegates/bug639751.vala    |    2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 09dad44..9fb3ca3 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3696,6 +3696,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		var result = ((GLibValue) value).copy ();
 
 		if (type is DelegateType) {
+			var delegate_type = (DelegateType) type;
+			if (delegate_type.delegate_symbol.has_target) {
+				Report.deprecated (node.source_reference, "copying delegates is discouraged");
+			}
 			result.delegate_target_destroy_notify_cvalue = new CCodeConstant ("NULL");
 			return result;
 		}
diff --git a/tests/asynchronous/bug597294.vala b/tests/asynchronous/bug597294.vala
index 92a04bf..2558460 100644
--- a/tests/asynchronous/bug597294.vala
+++ b/tests/asynchronous/bug597294.vala
@@ -1,6 +1,6 @@
 delegate void Foo ();
 
-async void do_foo (Foo f) {
+async void do_foo (owned Foo f) {
 	f ();
 }
 
diff --git a/tests/asynchronous/bug639591.vala b/tests/asynchronous/bug639591.vala
index 1c92edb..89504ff 100644
--- a/tests/asynchronous/bug639591.vala
+++ b/tests/asynchronous/bug639591.vala
@@ -1,6 +1,6 @@
 delegate void Deleg ();
 
-async void foo (Deleg deleg)
+async void foo (owned Deleg deleg)
 {
 	Deleg d = () => { deleg (); };
 	d = null;
diff --git a/tests/delegates/bug595639.vala b/tests/delegates/bug595639.vala
index 3278dfe..55ba11b 100644
--- a/tests/delegates/bug595639.vala
+++ b/tests/delegates/bug595639.vala
@@ -1,7 +1,7 @@
 delegate void Foo ();
 
-void do_foo (Foo foo) {
-	Foo bar = foo;
+void do_foo (owned Foo foo) {
+	Foo bar = (owned) foo;
 }
 
 void main () {
diff --git a/tests/delegates/bug639751.vala b/tests/delegates/bug639751.vala
index 055a6de..8c69a81 100644
--- a/tests/delegates/bug639751.vala
+++ b/tests/delegates/bug639751.vala
@@ -1,5 +1,5 @@
 struct Foo {
-	public SourceFunc bar;
+	public unowned SourceFunc bar;
 }
 
 void main() {



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