Last active
December 13, 2015 19:19
-
-
Save PhilipWitte/4962210 to your computer and use it in GitHub Desktop.
Memory Safety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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