0

This seems to appear on two controllers in our project. I checked both code and storyboards and the segue in the storyboard is NOT called in the controller, there is no IBAction nor IBOutlet related to the buttons that will trigger the segue. Though when i leave my controllers i can see from a print that the deinit method is called twice.

This is weird since viewDidLoad and viewWillDisappear are only called once, did anyone encounter this behaviour before? There is nothing related to a double deinit on SO and i would like very much to understand what is going on.

Edit : So, little update. I added this bit of code which i set a breakpoint on to check when the controller is init :

        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

}

required init(coder aDecoder: NSCoder) {
    print("init coder style")
    super.init(coder: aDecoder)!
}

So init coder is called ONCE when i trigger the segue to get to my controller, and when i tap on the ok button of said controller which is plugged to a popSegue, my controller is then init a second time (also with init coder) and then the double deinit happen.

So what happen is that an empty instance of my controller is created when i dismiss it with a popSegue. I fail to understand why though.

thibaut noah
  • 1,474
  • 2
  • 11
  • 39
  • Can you post relevant codes for both viewcontrollers? Its very hard to speculate what might be the reason behind it without any context or code. – idocode Feb 07 '17 at 16:42
  • The thing is, there is no code, there is only a UIButton which is connected to a segue leading to a storyboard reference. I am not calling performSegueWithIdentifier anywhere in the code nor do i do any manual instantiation. The only bit of code related to this controller is in prepareForSegue which i use to set a delegate if the identifier of the segue matches. Also the var which captures self to set the delegate is a weak var, so no reason for a strong reference cycle. – thibaut noah Feb 07 '17 at 16:49
  • "nor do i do any manual instantiation" I think you do. – matt Feb 07 '17 at 16:59
  • No i don't, i have check the code both manually and with the search feature on xcode, if i don't see the name of the controller appearing anywhere there is no way it is instantiated manually. Also there is no called to performSegueWithIdentifier so my segue is not called twice. – thibaut noah Feb 07 '17 at 17:04
  • I thought so too at first but unless i failed to find a proper method to check it... – thibaut noah Feb 07 '17 at 17:11
  • If you reference `self` inside `deinit`, it will be called twice – Nikunj Acharya Jun 19 '19 at 07:26

2 Answers2

3

My first thought is maybe you actually have two objects. Try setting a breakpoint on deinit and see if the pointer value for the object is the same both times.

Uncommon
  • 3,323
  • 2
  • 18
  • 36
  • There is indeed two objects, one being an empty instance, but i can't figure out where it is coming from. This object can only be init by the previous controller (since it is used to search) and the only thing anywhere in the app that will trigger an instantiation of this object is the segue in the storyboard. The segue is call in prepare for segue with : else if segue.identifier == "goToSearchArtist" { //custom segue self.navigationController?.delegate = arriveFromRightNavigationControllerDelegate } And that is all, no performSegueWithIdentifier. – thibaut noah Feb 07 '17 at 16:47
  • Is there an init method where you can set a breakpoint? If not, maybe you should write a temporary one for debugging. – Uncommon Feb 07 '17 at 16:49
  • I did just write one (thanks for the lead) and it confused me even more actually. – thibaut noah Feb 07 '17 at 17:05
  • Great call @Uncommon – Josh Bernfeld Dec 26 '17 at 09:03
0

Storyboard could be a culprit in this case. Try to remove your UIViewController subclass from the storyboard and create it programmatically. Deinit should be called only ones then.

Andrey Volobuev
  • 862
  • 8
  • 11
  • Unfortunatly i tried that too ! The second init is called during the first deinit. I have absolutly no clue of what is going on. – thibaut noah May 18 '17 at 14:11