Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

objective c - dismissViewControllerAnimated crash at ios5

Does the code crash, because of a circular reference?

MenuController: UIViewController

- (id)initWithNibName:
{...
TabsController *tabs = [[TabsController alloc] initWithNibName:@"TabsController" bundle:nil];
self.tab = tabs;
....
}

//button pressed:
- (IBAction)showPrefFromMenu:(id)sender {
    // todo change delegate!?
     tab.tabDelegate = self;
    [self presentModalViewController:tab animated:YES];
    //[tab release];
}

// delegate method:
 -(void)myViewDismissed {
    .... 
    NSLog(@"tab references: %d", [tab retainCount]) ;

    [self dismissModalViewControllerAnimated:YES];//crash       
     ...

}

the modal / child class:

TabsController : UIViewController <...>

- (IBAction)dismissTabs:(id)sender {
      ...
    NSLog(@"dismissTabs: sender: %@",sender);
    [self.tabDelegate myViewDismissed];    
}

As I see the self.tabDelegate is the MenuController instance and on that code want do dismiss and deallocate the TabsController.

Although it isn't any more code after [self.tabDelegate myViewDismissed]; but if it would be than couldn't execute, because it is deallocated, maybe the assembly Ret or what instruction can't be executed? the return statement.

I will try to separate the delegate or any better solution?

Edit: The crash is the typical one: EXC_BAD_ACCESS(code=1,address=090) the Assembly looks like this: ldr r1, [r4, r0]

Edit2: changed a bit the code, because in simulator 4.3 doesn't crash, but at 5.0 it is, now here is the current code:

- (IBAction)showTab:(id)sender {

    tab.tabDelegate = self;

    if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
        [self presentModalViewController:tab animated:YES];
    }
    else{
        NSLog(@"Executing presentViewController (ios>= 5.0)");
        [self presentViewController:tab animated:true completion: nil];
    }

}


 -(void)delegateCallback {

     if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
         [self dismissModalViewControllerAnimated:NO]; 
     }
     else{
         NSLog(@"Executing dismissViewControllerAnimated (ios>= 5.0)");
         [self dismissViewControllerAnimated:TRUE completion: nil];//crash
     }        

}

Edit3 screenshot:

threads

UIWindowController transition:fromViewController:toViewController:didEndSeelctor line is crashing, due to: no parentViewController: https://devforums.apple.com/message/451045 Guys here found a solution, : https://github.com/ideashower/ShareKit/issues/254 but in under NDA

Edit solved to revrite to PushviewController for ios 5.0+ a heplfull link: https://stackoverflow.com/a/7767767/529543

- (IBAction)presentViewController:(id)sender {


    tab.tabDelegate = self;

    if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
        [self presentModalViewController:tab animated:FALSE];
    }
    else{
        NSLog(@"Executing presentViewController (ios>= 5.0) [tab retainCount]: %d " ,[tab retainCount]);       

    // store parent view to able to restore the state:
    parentView = self.view.superview;        

    // init a navigation controler and set up:
    navigationController=[[UINavigationController alloc] initWithRootViewController:self];                
    [self.view removeFromSuperview];

    [myAppDelegate.window addSubview:navigationController.view];   ///appDelegate is delegate of ur Application     

    navigationController.navigationBar.hidden =true;

    [navigationController pushViewController:tab animated:YES];       

}

}

and popping:

-(void)infoViewDismissed {

     if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {

         [self dismissModalViewControllerAnimated:NO];  
     }
     else{
         NSLog(@"Executing dismissViewControllerAnimated (ios>= 5.0) ");

         [navigationController popToRootViewControllerAnimated:false];        

         [navigationController.view removeFromSuperview];

         [parentView addSubview:self.view];

     }     
}

I have solved my problem, in a very ugly mode, but is functional...Also told to drop the support for ios3 :) I don't like the GUI architecture switch at runtime at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is a little difficult to understand, but I gather you have a retain-cycle:

ObjectA retains ObjectB
ObjectB retains ObjectA

and neither object gets deallocated?

Your property for the tabDelegate should read:

@property (nonatomic, assign) id tabDelegate;
//                    ^^^^^^-This is the important bit, this stops the retain cycle.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...