Will this Vala code memory leak when it built as shared library (.so)?
Vala:
namespace test {
public static string info(string name){
return "Hello " + name;
}
}
Source code (valac -C
)
gchar* test_info (const gchar* name) {
gchar* result = NULL;
const gchar* _tmp0_ = NULL;
gchar* _tmp1_ = NULL;
g_return_val_if_fail (name != NULL, NULL);
_tmp0_ = name;
_tmp1_ = g_strconcat ("Hello ", _tmp0_, NULL);
result = _tmp1_;
return result;
}
Compilation: valac --library=test -H test.h "test.vala" -X -fPIC -X -shared -o test.so
I am surprised by no memory deallocation in test_info
.
g_strconcat
store allocated memory in global variable (possible thread-local)?test_info
from external programm miltiple times without deallocation?I am sorry about this possible simple question, but I am new in Vala (my primary experience in area of Go, Python, C++ and etc)
Your code will return an owned string, so the caller is responsible for the memory deallocation.
If you call this library function from vala the compiler will make sure that it is deallocated.
If you call it from C
you should read the GLib documentation for g_strconcat:
Concatenates all of the given strings into one long string. The returned string should be freed with g_free() when no longer needed.
I would recommend you to read:
See also this question (which is about Genie, Valas "sister language" though).