Ahoy there friends..
i am trying to maintain a list, which should not process if the item is a nil
using {.cursor} seem to do the trick of tracking garbage collection (arc, and orc)
but i am trying to crash it if i am working with a "released" reference, but it didn't crash
here is a code snippet
type GameObject = object
name* = "unnamed!"
valid* = true
value* = -1.BiggestUInt
proc inc*(self: ref GameObject) =
self.value += 1
proc `=destroy`*(obj: var GameObject) =
obj.valid = false
echo "Destroying Game Object: " & obj.name
block testing_lifetime:
echo "now testing lifetime"
var game_object_list: seq[ref GameObject]
block:
var a = new GameObject
a.name = "a!"
let b {.cursor} = a # when we set b as cursor
reset a # calling reset here will destroy a
# let b = a # but when b is not a cursor
# reset a # calling this should not destroy a.., and yes the {.cursor} pragma do it's job very nicely
game_object_list.add(b)
echo "is a destroyed before me?"
echo "length of the list: ", game_object_list.len
echo game_object_list[0].name, " valid? ", game_object_list[0].valid
game_object_list[0].inc() # though the ref is destroyed, we can still calls the method
echo game_object_list[0].value
it would be very nice if the compiler could automaticly provide if a cursor is "safe" to use..
i thank you before for your time friends!