[vala] Fix string switch statements with owned switch expression



commit bce013426fffe7baa80c2f2a7dbf2810285d54bb
Author: Jürg Billeter <j bitron ch>
Date:   Mon Sep 14 20:03:14 2009 +0200

    Fix string switch statements with owned switch expression
    
    Fixes bug 591879.

 codegen/valaccodecontrolflowmodule.vala |   10 +++++++++-
 vala/valaswitchstatement.vala           |    3 +++
 2 files changed, 12 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index 19a30a0..cb1b8c2 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -44,13 +44,16 @@ internal class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
 	void visit_string_switch_statement (SwitchStatement stmt) {
 		// we need a temporary variable to save the property value
-		var temp_var = get_temp_variable (stmt.expression.value_type, false, stmt);
+		var temp_var = get_temp_variable (stmt.expression.value_type, stmt.expression.value_type.value_owned, stmt);
 		stmt.expression.temp_vars.insert (0, temp_var);
 
 		var ctemp = new CCodeIdentifier (temp_var.name);
 		var cinit = new CCodeAssignment (ctemp, (CCodeExpression) stmt.expression.ccodenode);
 		var czero = new CCodeConstant ("0");
 
+		var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
+		free_call.add_argument (ctemp);
+
 		var cswitchblock = new CCodeFragment ();
 		stmt.ccodenode = cswitchblock;
 
@@ -93,6 +96,11 @@ internal class Vala.CCodeControlFlowModule : CCodeMethodModule {
 		cswitchblock.append (new CCodeExpressionStatement (cinit));
 		create_temp_decl (stmt, stmt.expression.temp_vars);
 
+		if (stmt.expression.value_type.value_owned) {
+			// free owned string
+			cswitchblock.append (new CCodeExpressionStatement (free_call));
+		}
+
 		Gee.List<Statement> default_statements = null;
 		label_count = 0;
 
diff --git a/vala/valaswitchstatement.vala b/vala/valaswitchstatement.vala
index a66a284..d1468e5 100644
--- a/vala/valaswitchstatement.vala
+++ b/vala/valaswitchstatement.vala
@@ -114,6 +114,9 @@ public class Vala.SwitchStatement : CodeNode, Statement {
 			return false;
 		}
 
+		// ensure that possibly owned (string) expression stays alive
+		expression.target_type = expression.value_type.copy ();
+
 		foreach (SwitchSection section in sections) {
 			section.check (analyzer);
 		}



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