String memory of out parameter leaked on throw
In the following code:
errordomain FooError { FOO }
void foo (out string s) throws FooError {
s = "abc";
throw new FooError.FOO ("FOO");
}
int main(string[] args) {
string s;
try {
foo (out s);
} catch (FooError e) {
}
return 0;
}
the memory for s
in foo ()
is leaked:
$ valac-0.52 -g -C foo.vala
$ gcc -g3 -fsanitize=address -fsanitize=undefined foo.c -o foo `pkg-config --cflags --libs glib-2.0 gobject-2.0`
$ ./foo
=================================================================
==4010549==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4 byte(s) in 1 object(s) allocated from:
#0 0x7fea0815abc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
#1 0x7fea07f7bcd8 in g_malloc (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x57cd8)
#2 0x7fea07f95f93 in g_strdup (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x71f93)
#3 0x5646c1f004a1 in foo /home/rrt/Software/zile/src/foo.vala:4
#4 0x5646c1f00916 in _vala_main /home/rrt/Software/zile/src/foo.vala:11
#5 0x5646c1f00f48 in main /home/rrt/Software/zile/src/foo.vala:8
#6 0x7fea073ec0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#7 0x5646c1f002ed in _start (/home/rrt/Software/zile/src/foo+0x22ed)
I am using current git master HEAD valac.
I searched for other issues tagged leak
and tried to find any suggestion in the documentation that this is expected behaviour, and drew a blank on both fronts.
As a workaround, setting s = null
before the throw
fixes the leak.