1

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.

Community
  • 1
  • 1

2 Answers2

3

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.
James Webster
  • 30,976
  • 11
  • 64
  • 113
  • I have / had : @property (assign) id tabDelegate; adding nonatimic, doesn't help, but thanks –  Aug 07 '12 at 07:46
  • changed to Retain, but doesn't helped me either –  Aug 07 '12 at 08:05
  • Retain is bad for use with the delegate pattern. Are you using ARC? – James Webster Aug 07 '12 at 08:06
  • in this project isn't used ARC. Also Emable zoombies doesn't help me which is the deallocated object. The tab bar has tabs ofc... I aftaif in one of the tabs has wrong memory management and maybe there is an deallocated object, but there are hundred of objecs, ivars on those tabs... –  Aug 07 '12 at 10:16
1

tough to tell without more info (are you using ARC, are you retaining/assigned the delegate, etc...) but per the iOS docs you are also using deprecated modalview methods. May be worth trying:

[self presentViewController:tab animated:YES completion:NULL];

and

[self dismissViewControllerAnimated:YES completion:NULL];
Will Ayd
  • 5,903
  • 1
  • 32
  • 38
  • thanks, it is marked it will be deprecated, it isn't now. Also doesn't change the behavior, it will crash. I am appreciating the effort to help me, that is why I am markig with UP sign. buy DOES'T change. –  Aug 07 '12 at 07:30
  • ah OK. per your last edit i see the issue is a bad access error are you calling release on the TabsController anywhere else in your code? Does XCode give you any messages when using "Product" > "Analyze"? – Will Ayd Aug 07 '12 at 11:45
  • xcode analyze, around 45 leak it says, but I can't trust that report, altough it can be a starting point –  Aug 07 '12 at 12:20
  • 1
    oh wow that is a lot of leaks. although analyze may not find everything in your application the fact that it is pointing out 45 leaks is very concerning. i would suggest spending more time looking through the apple documentation reading up on memory management and then coming back to re-writing the application – Will Ayd Aug 07 '12 at 12:37
  • those leaks indicating it is a memory allocation, which aren't used. My problem it is exactly the opposite: it is deallocated an object and I am using, so memory leak reports are completely useless for me –  Aug 07 '12 at 14:18
  • 2
    If the analyzer says there are 45 leaks, your memory management is broken. No surprise you have an over release. Fix the analyzer warnings first, then debug the remaining memory issues. The analyzer absolutely can be trusted. – bbum Aug 08 '12 at 12:49
  • I wouldn't believe I need to rewrite, but I couldn't upkeep the code. It was crashing in one side or in other side. I took me like 3 days to re-make the skeleton of GUI, workflow a few hours to escape from major leaks. The app is a bit big and was developed 2 years ago by somebody else. Now is rewritten all with push/popviewControler. With model some methods will be deprecated with iOS6 –  Aug 26 '12 at 09:38