Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created April 6, 2016 04:03
Show Gist options
  • Save onevcat/3da91b1b1c9409c9e57536a5736396f0 to your computer and use it in GitHub Desktop.
Save onevcat/3da91b1b1c9409c9e57536a5736396f0 to your computer and use it in GitHub Desktop.
AnyGenerator Leak?
let count = 3
var current = 0
let g = AnyGenerator<Int>(body: {
if current < count {
current += 1
return current - 1
} else {
return nil
}
})
while let num = g.next() {
print(num)
}
@onevcat
Copy link
Author

onevcat commented Apr 6, 2016

// This will also leak in Release build
let g = AnyGenerator<Int>(body: { return nil })

// But manipulate the generator by wrapping it in an array for example would fix the leak:
let g = AnyGenerator<Int>(body: { return nil })
let _ = Array(g)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment