Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deinit never called

I'm creating a ViewController object an pushing it to a navigation controller. When the object is being popped from the stack - it is not being release and Deinit is not being called. What can be the reason for that?

Here's the code that pushes:

self.navigationController?.pushViewController(CustomViewController(), animated: true) 

And here's the code that pops:

 self.navigationController?.popViewControllerAnimated(true) 
like image 583
YogevSitton Avatar asked Nov 17 '14 11:11

YogevSitton


People also ask

Why is Deinit not called?

So the deinit is not called if there is no additional scope.

How do you call a Deinit in Swift?

Before a class instance needs to be deallocated 'deinitializer' has to be called to deallocate the memory space. The keyword 'deinit' is used to deallocate the memory spaces occupied by the system resources. Deinitialization is available only on class types.

How do I remove Viewcontroller from memory?

You can't remove a view controller from within itself (i.e. viewDidDisappear) - what you need to do is to remove all references to it, at which point ARC will deallocate it.


1 Answers

I had similar problem. I added empty deinit method to my class and added breakpoint:

deinit {  } 

As result it's never called.
As soon as I add some code to the body it started working as expected:

deinit {     print("deinit called") } 

So be sure that your deinit method isn't empty.
PS. I am using Swift 2, Xcode 7.1

like image 105
Vlad Papko Avatar answered Sep 30 '22 21:09

Vlad Papko