type
Object* = ref object of RootRef
impl*: pointer
type
InitiallyUnowned* = ref object of Object
type
Widget* = ref object of InitiallyUnowned
proc finalizeGObject*[T](o: ref T) =
echo "finalizeGObject"
proc main =
var w: Widget
new(w, finalizeGObject)
echo "OK"
var p: pointer
proc m1 =
var w1: Widget
new(w1, finalizeGObject)
p = cast[pointer](w1) # store Nim object in fake C lib
GC_ref(w1) # we have to keep it alive
echo "OK"
proc m2 =
var w2: Widget
w2 = cast[Widget](p) # get Nim object back from C lib
GC_unref(w2) # when this is the last access then GC can collect it
echo "OK"
main()
m1()
m2()
nim c --gc:arc -r t.nim
Hint: /tmp/hhh/t [Exec]
OK
finalizeGObject
OK
OK
finalizeGObject
This indeed works like I hoped it would.