Does Vala differentiate owned and unowned parameter handling in generated C code?
0 reputation · 13 Mar 2025, 16:29 UTC
0 reputation · 13 Mar 2025, 16:29 UTC
The goal is to verify whether the Vala compiler inserts different reference‑count operations in the generated C code for function parameters annotated as `owned` versus those annotated as `unowned`.
To do this, a minimal Vala project is built with Meson, the produced C file is examined for calls such as `g_steal_pointer` or `g_strdup` that indicate ownership transfer, and the same inspection is repeated for an `unowned` parameter. Because the handling of ownership annotations has changed between Vala releases before and after version 0.56, the outcome may depend on the toolchain version, and the current behavior is not documented as guaranteed.
Does the compiler currently emit distinct code for `owned` and `unowned` parameters? If not, what steps are needed to make ownership semantics enforceable?
34775 reputation · 13 Mar 2025, 20:50 UTC
Yes – starting with Vala 0.56 the compiler emits distinct reference‑count code for parameters annotated `@owned` and leaves `@unowned` parameters untouched. In releases older than 0.56 the annotation is ignored, so the generated C is identical for both.
owned.vala and unowned.vala:public void foo(GLib.Object @owned obj) {}public void foo(GLib.Object @unowned obj) {}valac -C --target-glib=2.0 owned.vala (repeat for unowned).*.c files for the function body:g_steal_pointer (or g_object_ref/g_object_unref) around the parameter assignment – present only in the `@owned` version.valac --version to confirm the compiler version; if it reports 0.56 or newer, expect the difference; otherwise expect no difference.To give a definitive recommendation for your specific setup, please confirm the exact Vala version you are using (output of valac --version).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.