release view controller and removing view

0

Quick question:

If I use

[someViewController.view addSubView:otherViewController.view];

to add a view. And then use

[otherViewController.view removeFromSuperView] 

to remove the view, my app will crash when I call [otherViewController release]

The crash point is in the [super dealloc] line in my dealloc method of otherViewControll class implementation.

otherViewController is a reference to the view controller. I call release after its view has been removeFromSuperView'ed. By the time I call release, it's a valid pointer.

What am I doing wrong here?

otherViewController's dealloc class implementation

- (void)dealloc {
    [popVC release];
    [photoContainer release];
    [photoView release];
    [recordName release];
    [recordIngr release];
    [recordDesc release];
    [recordPrice release];
    [quantity release];
    [pricingLabel release];
    [increaseButton release];
    [decreaseButton release];

    [pricingTableVC release];
    [pricingTable release];

    [super dealloc];   // <--- crash point
}

updated: call trace

2011-06-04 00:35:05.110 MyApp[2308:207] -[__NSCFType _viewDelegate]: unrecognized selector sent to instance 0x4b6feb0
2011-06-04 00:35:05.124 MyApp[2308:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType _viewDelegate]: unrecognized selector sent to instance 0x4b6feb0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dd75a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f2b313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dd90bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d48966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d48522 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x00379051 -[UIViewController dealloc] + 128
    6   MyApp                              0x00009b26 -[RecordDetailViewController dealloc] + 797
    7   MyApp                              0x00004744 __-[RecordRootViewController bringUpNextRecordDetail:isNext:]_block_invoke_2 + 77
    8   UIKit                               0x002f7fb9 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 294
    9   UIKit                               0x002f7e4b -[UIViewAnimationState animationDidStop:finished:] + 77
    10  QuartzCore                          0x01d7b99b _ZL23run_animation_callbacksdPv + 278
    11  QuartzCore                          0x01d20651 _ZN2CAL14timer_callbackEP16__CFRunLoopTimerPv + 157
    12  CoreFoundation                      0x00db88c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    13  CoreFoundation                      0x00db9e74 __CFRunLoopDoTimer + 1220
    14  CoreFoundation                      0x00d162c9 __CFRunLoopRun + 1817
    15  CoreFoundation                      0x00d15840 CFRunLoopRunSpecific + 208
    16  CoreFoundation                      0x00d15761 CFRunLoopRunInMode + 97
    17  GraphicsServices                    0x0172e1c4 GSEventRunModal + 217
    18  GraphicsServices                    0x0172e289 GSEventRun + 115
    19  UIKit                               0x002d5c93 UIApplicationMain + 1160
    20  MyApp                              0x0000200c main + 102
    21  MyApp                              0x00001f9d start + 53
)
terminate called after throwing an instance of 'NSException'

Update: in -viewDidLoad, I have a gesture recognizer:

{
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showNextRecod:)];
    [leftSwipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer];
    [leftSwipeGestureRecognizer release];

}

I tried to use a Button to call -(IBAction) showNextRecod, it won't crash!! Only when I use the gesture to call the same method, it would crash

Regards

Leo

iphone
objective-c
cocoa-touch
ipad
asked on Stack Overflow Jun 3, 2011 by leo • edited Jun 3, 2011 by leo

3 Answers

3

When you add a view controller's view as a subview to another view, you are only retaining it's view in memory and not the controller itself. Therefore, you must retain the view controller somewhere else (most likely make it a property)

Does that make sense?

answered on Stack Overflow Jun 3, 2011 by brandontreb
1

In your gesture selector showNextRecod:
you should remove your target [pGestureRecognizer removeTarget:nil action:NULL];

answered on Stack Overflow Dec 10, 2012 by CiNN
1

@leo You are just adding otherViewController.view in someViewController.view.

Just adding view not allocating it, then just remove it from your view.

Im not getting why you are using [otherViewController release] when you are not allocating it same view.

For sure you can log otherViewController.view count. See how much retain count for that view you are getting.

answered on Stack Overflow Dec 10, 2012 by pallavi

User contributions licensed under CC BY-SA 3.0