I have a subclass of a view controller. After adding the view of this view controller to another view, any interaction with the view of MyViewController causes a crash. When running zombies it shows a double release, the retain count of myViewController has gone to -1.
I am calling alloc on this object, which should bring the retain count to 1, and I am also adding the view of myViewcontroller to another view, which should bring the retain count to 2. So how am I getting a double release? This only crashes on ARC
- (void)viewDidLoad {
[super viewDidLoad];
MyViewcontroller *myViewcontroller = [[MyViewController alloc] init];
[self.view addSubview:myViewcontroller.view];
}
There’s no “double” release here, just the one.
You’re storing the view controller in a local variable here. ARC correctly releases it when this method ends — to do otherwise would cause a leak because you won’t have a reference to it anymore.*
No;
addSubview:takes ownership of the view, not the controller. The view itself will still be alive later, but the controller won’t.*So, interestingly, this would not crash under MRR, but would be incorrect.