Why is this code crashing my program

Discussion in 'iOS Programming' started by cis4life, Jun 17, 2009.

  1. cis4life macrumors regular

    Joined:
    Apr 4, 2008
    #1
    I have this code snippet but its crashing the application.

    Code:
    
    - (void)viewDidLoad 
    {
       BlueViewController *blueController = [[BlueViewController alloc]   initWithNibName:@"BlueView" bundle:nil];
       self.blueViewController = blueController;
       [self.view insertSubview:blueController.view atIndex:0];
       [blueController release];
    }
    
    
    This line in the code is making the app crash...
    [self.view insertSubview:blueController.view atIndex:0];

    if i commet it out, the app works, when I comment it back in, it breaks?

    Why?

    Cedric
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Crashes how? Are you getting a run-time error? We need more details.
     
  3. cis4life thread starter macrumors regular

    Joined:
    Apr 4, 2008
    #3
    the programs just stops and says it stopped unexpectedly. When I remove that line, the program loads up, but of course the view doesn't load (i commeted out that line)

    Cedric
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Probably because you are trying to insert a UIViewController instead of a UIView. Check the documentation for UIView.
     
  5. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #5
    Try

    [self.view insertSubview:[self.blueViewController view] atIndex:0];
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    is that line you posted the exact line the app CRASHES or is it just the one causing your crash?

    because I guess the release-line is the real bad guy. Because insertSubview probably sends a retain message to the VIEW, but not the viewController ... or so I guess. something like that.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    No error messages or hints in the crash log?
     
  8. NeilBright macrumors newbie

    Joined:
    Jun 10, 2010
    #8
    Hi,

    Did anyone find a solution to this?

    Thanks.
     
  9. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #9
    Is it possible that in the nib the view controller isn't connected to the view?
     
  10. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #10
    I second this suggestion. I believe insertSubview:nil will crash.

    A more useful thing to do would be to run in the simulator using GuardMalloc and post everything that appears on the console.
     

Share This Page