Nib-created view retaining file's owner prevents deallocation of UIViewController?

Assuming rvc is a retain property you have a leak . That's why the controller is not getting dealloc ated. When you create the view controller you are over-retaining it.

Up vote 3 down vote favorite share g+ share fb share tw.

I have a view controller that I want to be released (and then possibly reallocated later in the application). The problem is that its view seems to hold a strong reference to it as shown in the stack trace below. Does the view need to be deallocated before the view controller?

If so, how can I do this? Thanks for the help :) Code: - (void)setCurrentCourse:(Course *)newCourse { ... Reference count management ... // area of concern if (currentCourse == nil) { self.rvc. View removeFromSuperview; self.

Rvc release; // HERE I want rvc to be deallocated, but the retainCount is one. } else { // This is where I allocate the rvc instance self. Rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self; self.

View addSubview:self.rvc. View; } } Backtrace from overriden -(id)retain; #0 -RootViewController retain (self=0x1bb610, _cmd=0x349b6814) at RootViewController. M:609 #1 0x340b1cdc in CFRetain () #2 0x341620c0 in __CFBasicHashStandardRetainValue () #3 0x34163440 in __CFBasicHashAddValue () #4 0x340b1ff8 in CFBasicHashAddValue () #5 0x340b6162 in CFSetAddValue () #6 0x340c3012 in -__NSCFSet addObject: () #7 0x348cb70c in -UINib instantiateWithOwner:options: () #8 0x348cce08 in -NSBundle(UINSBundleAdditions) loadNibNamed:owner:options: () #9 0x348465e8 in -UIViewController _loadViewFromNibNamed:bundle: () #10 0x34813fa4 in -UIViewController loadView () #11 0x346f8ebe in -UIViewController view () objective-c ios cocoa-touch uiviewcontroller nib link|improve this question edited Oct 11 '11 at 23:21 asked Oct 11 '11 at 23:14Michael656 70% accept rate.

– chown Oct 11 '11 at 23:16 @chown okay I added it – Michael Oct 11 '11 at 23:22.

Assuming rvc is a retain property, you have a leak. That's why the controller is not getting deallocated. When you create the view controller you are over-retaining it: self.

Rvc = RootViewController alloc initWithNibName:...; alloc returns a retained object (+1). Then, the property setter also retains the object (+2). Later, when you release (-1) the object, you end up with a +1.

To solve this, either use a temporary variable or autorelease: self. Rvc = RootViewController alloc initWithNibName:... autorelease; Another concern is the way you release the object that your property is holding to: self. Rvc release; After this statement, you have relinquished ownership of the object and nothing guarantees you that the object will be valid in the future but your property is still holding a pointer to it.

In other words, you have a potential dangling reference. So, nil out the property as you release it with this single statement (this will release the old object): self. Rvc = nil.

Thanks. So...should I rename my question? :D – Michael Oct 12 '11 at 0:58 You are welcome.

I don't think you should rename your question since it reflects your original thoughts. – albertamg Oct 12 '11 at 10:19.

Change self. Rvc release; to rvc release;: - (void)setCurrentCourse:(Course *)newCourse { // area of concern if (currentCourse == nil) { self.rvc. View removeFromSuperview; rvc release; // HERE I want rvc to be deallocated, but the retainCount is one.

} else { // This is where I allocate the rvc instance rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self; self. View addSubview:self.rvc. View; } } or use self.

Rvc = nil; because when you set nil as an instance variable, the setter just retains nil (which does nothing) and releases the old value. And use rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self; instead of self. Rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self.

Thanks for your answer. My issue persists. What's the problem with self.

Rvc release? – Michael Oct 12 '11 at 0:02 Try changing self. Rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self; to rvc = RootViewController alloc initWithNibName:@"RootViewController" bundle:NSBundle mainBundle courseSelectionController:self; also.

– chown Oct 12 '11 at 0:06 self. Calls the auto generated setter code, which retains whatever is passed, so alloc'ing something, then sending it to self. Property gives it a retain count of 2, instead of 1 like you want.

See albertamg's answer for a more detailed explanation. – chown Oct 12 '11 at 0:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions