Skip to content

3.4. Binding to C Heap Handlers

One of the unique features of Vala is to have both singly-owned instances and reference-counted instances. Reference-counted instances can be stored in new locations and memory management done by counting the number of references; destruction of the instance is done when there are no more references to that instance. Singly-owned instances have a single authoritative reference and, when that reference is destroyed, the instance is destroyed. Reference-counted objects can thus be "copied" by increasing the reference count while singly-owned instances cannot be copied without duplicating the actual data in them, if that is even possible.

While this is primarily a concern for objects, all instances in Vala must subscribe to one of these memory management schemes. Different types of objects can follow different schemes and some types can subscribe to different schemes depending on subtle differences in declaration.

Vala TypeSchemeC TypeMemory Management Binding Needed?
Enum and FlagValueintNo
Delegate (has_target = false)ValueFunction PointerNo
Delegate (has_target = true)ValueFunction Pointer and Void PointerNo
Delegate (has_target = true)Singly-OwnedFunction Pointer and Void PointerYes, use free_function
Simple-Type StructValueVarious Basic Types or a StructNo
StructValueStruct, but passed as a Pointer to a StructNo
StructParentedStruct, but passed as a Pointer to a StructYes, use destroy_function
Compact ClassSingly-OwnedPointer to a StructYes, use free_function
Compact ClassReference-CountedPointer to a StructYes, use ref_function and unref_function
PointerValuePointer to ContentsNo
ArraySingly-OwnedPointer to Element Type (and Integer Length)Yes, use free_function