3

If i create an new "Single View Application" project with xcode(6.3.2) and add only a deinit function to the ViewController. Run app, close app. Deinit is never called! I tested it with several devices and the simulator.

deinit {
    println("deinit:")
}

Why is that? Is that the standard behaviour? What can i do to make it work?

/* Edit */ After reading the comments i put this code to my Viewcontroller.

import UIKit

class A {
    func test() {
        println("A test")
    }

    deinit {
        println("A deinit")
    }
}

class ViewController: UIViewController {

    var a:A? = A()

    override func viewDidLoad() {
        super.viewDidLoad()
        println("ViewController viewDidLoad")
        a?.test()
    }

    override func viewDidDisappear(animated:Bool) {
        println("ViewController viewDidDisappear")
        a = nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        println("ViewController didReceiveMemoryWarning")
    }

    deinit {
        println("ViewController deinit")
        a = nil
    }
}

Now i get

ViewController viewDidLoad:
A test
ViewController viewDidDisappear:
A deinit

Does this mean that i have to cleanup my objects by hand within the viewDidDisappear call because the view controllers deinit function is not reliable?

loop masta
  • 149
  • 11
  • 2
    Is there a point in time where this ViewController is going out of memory? Do you replace this viewController with another one? – Shamas S Jun 29 '15 at 09:35
  • No. All i did was to add deinit. Nothing else. You can test it by creating a new "Single View Application" project. Then add deinit with a println and / or a breakpoint. – loop masta Jun 29 '15 at 13:20
  • 3
    possible duplicate of [Swift: no output for println in deinit method (not using playground)](http://stackoverflow.com/questions/27044573/swift-no-output-for-println-in-deinit-method-not-using-playground) – Rob Napier Jun 29 '15 at 13:34
  • Rob is right it is a duplicate – alinoz Jun 29 '15 at 13:56

2 Answers2

1

No. In this case the ViewController deinit isn't even being called because ViewController doesn't go out of memory.

A way to test this is to create a new ViewController and replace the current ViewController with it. This should remove your current ViewController out of the memory, hence calling it's deinit method.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
1

As apple states in documentation, App termination is done via SIGKILL that it a lot faster than exiting via sending message.

So no code will be called, neither applicationWillTerminate, for example.

try this code:

//  CustomView.swift
import UIKit

class CustomView: UIView {

    deinit{
        print("Swift CustomView dying...\n")

    }
}

in controller:

var v : CustomView?
        v = CustomView(frame: CGRectZero)

        v = nil

You will we in console:

Swift CustomView dying...

A Sample project is available on request. (write me)

ingconti
  • 10,876
  • 3
  • 61
  • 48
  • I wonder what happens to my memory if I allocate a memory chunk via `UnsafeMutablePointer` and don't use an optional type for my class (that holds the pointer) and the application terminates?! Do I get a memory leak, if so how do I solve this, because `deinit` is never called when my (command line) application terminates without any errors. – DevAndArtist May 30 '16 at 11:08
  • It would be interesting/helpful to have a URL for where "apple states in documentation, App termination is done via SIGKILL" … the behavior mentioned here appears to also be/remain the case for a SwiftUI `App`. It's been [elusive](https://developer.apple.com/search/?q=%2B%22SigKill%22) to find a documented connection between "SIGKILL" and normal App termination. – marc-medley May 21 '23 at 03:01