Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active December 13, 2015 19:19
Show Gist options
  • Save PhilipWitte/4962210 to your computer and use it in GitHub Desktop.
Save PhilipWitte/4962210 to your computer and use it in GitHub Desktop.
Memory Safety
type Foo
{
var bar = 0
}
func main()
{
ref f : Foo
ref b : int
scope
{
var foo = Foo # allocates Foo
f => foo # f points to foo
f.bar = 1 # foo.bar = 1
b => f.bar # b points to foo.bar
b = 3 # foo.bar = 3
# auto 'clean up code' injected at end of scope:
#
# Memory.delete(foo)
# f => null
#
# Notice: We don't need to set 'b' to null, since
# it's not being directly accessed after this point.
}
f.bar = 4 # ERROR: f is null
var n = 6
b => n
b = 7 # n = 7
# No clean-up code needed, 'n' is stack-allocated
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment