iOS 7.1 removeFromSuperview crash

后端 未结 9 977
孤街浪徒
孤街浪徒 2021-02-05 13:40

My app didn\'t have any crash until iOS 7.1 came out. Now on any removeFromSuperview method, crash. For example: I got view controllers, and when I

相关标签:
9条回答
  • 2021-02-05 14:27

    UPDATED

    try to do this:

    NSArray *subviews = [NSArray arrayWithArray:[contentVc subviews]];
    for (UIView *subView in subviews)
         [subView removeFromSuperview];
    

    I think that you got the crash beacuse you're trying to fast enumerate an array that has variable length (in fact when you remove a subview, it is removed also from subview array).

    If you want to remove the viewcontroller, just call:

    [contentVc.view removeFromSuperView];
    [contentVc removeFromParentViewController];
    
    0 讨论(0)
  • 2021-02-05 14:32

    1.According to the Apple's documentation, calling removeFromSuperview will remove that view from superview and release it automatically.

    So if you are using removeFromSuperview, then you should not call the [removedView release], which will crash your App.

    Refer this screenshot from Apple. 

    enter image description here

    In your dealloc implementation, you are having like so

    - (void) dealloc {
    
        // Removed from Parent view and released.
        [mainView removeFromSuperview];
    
        // no mainView exists in memory , so it crashed the App.
        [mainView release];// Comment this line to avoid the crash
    
        [super dealloc];
    }
    

    2.You should not mute the container that are being enumerated.

    You are having like this,
    
    for (UIView *subView in [contentVc subviews])
         [subView removeFromSuperview];
    

    Instead you can implement the same effect by having this one line from Apple.

    [[contentVc subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    0 讨论(0)
  • 2021-02-05 14:33

    please do the following:

    1- debug the for (UIView *subView in [contentVc subviews]) and check how many times it iterate.

    if it doesn't crash in the first hit you can add this line before you remove the view if (subView.superView != nil)

    else try to make sure that you are not releasing the views twice somewhere else as it's keep showing and will not crash till you remove it from it's superview.

    UPDATE2: i will consider that you are will aware of memory leaks, and that you have good experience in it.

    • whenever you add a subview to a view, this will retain the object by 1 in addition to the original 1, that will equal 2, then directly after adding the subview you have to release it, which will decrement the retain count back to one. here is the trick: you don't have to release the subview or remove it from it's parent view to get rid of the remaining retain count. you can simply remove or release the parent view. get the NSMutableArray As an example.

    • remove the [mainView removeFromSuperview]; from the dealloc: method. you can add it else where like viewWillDisappear: method. dealloc method shouldn't contain anything other than the release calls.

    0 讨论(0)
提交回复
热议问题