]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
GError: Fix leak when throwing an error in the constructor
authorThijs Vermeir <thijsvermeir@gmail.com>
Fri, 30 Oct 2009 22:46:34 +0000 (23:46 +0100)
committerJürg Billeter <j@bitron.ch>
Wed, 27 Jan 2010 20:46:47 +0000 (21:46 +0100)
Fixes bug 567818.

codegen/valagerrormodule.vala
tests/Makefile.am
tests/errors/bug567818.vala [new file with mode: 0644]

index ef25c067a87e4c4b6fd504fe7ada6470791a638e..b00438f12cccfde7f70d83a568942b79dd4eb3fd 100644 (file)
@@ -116,6 +116,10 @@ internal class Vala.GErrorModule : CCodeDelegateModule {
                cerror_block.add_statement (free_frag);
 
                if (current_method is CreationMethod) {
+                       var cl = current_method.parent_symbol as Class;
+                       var unref_call = new CCodeFunctionCall (new CCodeIdentifier (cl.get_unref_function ()));
+                       unref_call.add_argument (new CCodeIdentifier ("self"));
+                       cerror_block.add_statement (new CCodeExpressionStatement (unref_call));
                        cerror_block.add_statement (new CCodeReturnStatement (new CCodeConstant ("NULL")));
                } else if (current_method != null && current_method.coroutine) {
                        cerror_block.add_statement (new CCodeReturnStatement (new CCodeConstant ("FALSE")));
index e9628cd96517b16cd1c1cc04da5768dd84723378..783cd2116a92f148da63fce98df2ade3716d26f5 100644 (file)
@@ -62,6 +62,7 @@ TESTS = \
        objects/bug597155.vala \
        objects/bug597161.vala \
        errors/errors.vala \
+       errors/bug567818.vala \
        errors/bug579101.vala \
        errors/bug596228.vala \
        asynchronous/bug595735.vala \
diff --git a/tests/errors/bug567818.vala b/tests/errors/bug567818.vala
new file mode 100644 (file)
index 0000000..3700d78
--- /dev/null
@@ -0,0 +1,24 @@
+GLib.List<Foo> list;
+
+errordomain Error {
+       FOOBAR,
+}
+
+class Foo : Object {
+       public Foo () throws Error {
+               list.append (this);
+               throw new Error.FOOBAR ("foo");
+       }
+}
+
+void main () {
+       Foo foo = null;
+       list = new List<Foo> ();
+       try {
+               foo = new Foo ();
+       } catch (Error err) {
+       }
+       assert (foo == null);
+       /* There should be only 1 ref in the list */
+       assert (list.nth_data (0).ref_count == 1);
+}