My Swift globals are not getting deinitialized.
class Person {
let name: String
init(name: String) {
self.name = name
println("\(name) is being initialized")
}
deinit {
println("\(name) is being deinitialized")
}
}
func local() {
let person = Person(name: "Local")
}
local()
var personp: Person? = Person(name: "Optional Global")
personp = nil
var person = Person(name: "Global")
I am running this in a standalone binary (because apparently the playground has issues with deinit) with optimizations disabled, using Xcode6-Beta3:
> xcrun swift -O0 arc.swift && ./arc
Local is being initialized
Local is being deinitialized
Optional Global is being initialized
Optional Global is being deinitialized
Global is being initialized
Note the missing Global is being deinitialized.
I can't even figure out if this is expected behaviour or a bug, so if it is the former then references to the relevant legalese will be appreciated.
it looks fine to me... on ending the app nothing is deallocated - there is no point
deinit is only meant to FREE memory and remove observers and stuff - upon ending the process though, this is kinda 'useless' as the process memory 'will be wiped' anyways
==>
SO:
never put anything but memory management / observer related stuff in deinit
If you need a dedicated stop method - write one and call it explicitly before quitting the process
Think of the last line of code as:
var personp: Person? = Person(name: "Optional Global")
personp = nil
var person = Person(name: "Global")
exit(0)
Since person
is never set to another value, ARC never decreases the retain count before the exit.
Swift works like a C program in that execution simply terminates and then all memory allocated to the process is returned in one sweep.
This is very different from in-execution memory handling which relies on in-process events to free memory. Since all execution of the program has completely stopped, there is no thread to run the deinit
.
So in conclusion, this is just as it should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With