iPhone app crashing

Discussion in 'iOS Programming' started by Duke Leto, Mar 28, 2008.

  1. macrumors regular

    Joined:
    Mar 17, 2008
    #1
    Before you tell me that I have not enough experience to be programming, know that I have learned.

    I made a little iPhone application that goes from one tableview to another using view controllers. When I attempt to go back, it crashes.

    What have I done?
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    I doubt anyone would tell you that :). We all need to start somewhere.

    Questions like this are impossible to answer without more information in the form of source code I am afraid.
     
  3. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #3
    I can send in some code if it helps :)

    What I need to know before I do this is where the problem is likely to be: in the ViewController that cannot go back, or the original ViewController?
     
  4. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    As I said it is impossible to answer without seeing your source code. You could have made a mistake anywhere. Without seeing any code, any answer would just be a guess.
     
  5. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #5
    I am going to try to fix it on my own. The thing is, I had it working, then I accidentally pressed undo and it started crashing. GAH I hate this.
     
  6. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
  7. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #7
    This is what it says

    Exception Type: EXC_BREAKPOINT (SIGTRAP)
    Exception Codes: 0x0000000000000002, 0x0000000000000000
    Crashed Thread: 0

    Application Specific Information:
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSFastEnumerationEnumerator setAlpha:]: unrecognized selector sent to instance 0x1297420'
     
  8. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #8
    What you have all been waiting for *drumroll*


    Code:
    @implementation UserNameViewController
    
    @synthesize detailItem;
    @synthesize tableView;
    
    - (id)init
    {
    	if (self = [super init]) {
    		self.title = @"Accounts";
    
    	}
    	return self;
    }
    
    - (void)loadView
    {
        tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    	tableView.delegate = self;
    	tableView.dataSource = self;	
    	tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    	
    	self.view = tableView;
    }
    
    - (void)dealloc
    {
    	tableView.dataSource = nil;
    	tableView.delegate = nil;
    	[passwordViewController release];
    	[tableView release];
    	[super dealloc];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	return [[detailItem objectForKey:@"accounts"] count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withAvailableCell:(UITableViewCell *)availableCell {
    	
    	UISimpleTableViewCell *cell = nil;
    	if (availableCell != nil) {
    		cell = (UISimpleTableViewCell *)availableCell;
    	} else {
    		cell = [[[UISimpleTableViewCell alloc] init] autorelease];
    	}
    
    	// Set the value in the cell
    	NSArray *usernames = (NSArray *)[detailItem objectForKey:@"accounts"];
    	NSDictionary *account = (NSDictionary *)[usernames objectAtIndex:indexPath.row];
    	cell.text = [account objectForKey:@"username"];
    	return cell;
    }
    
    - (void)tableView:(UITableView *)tableView selectionDidChangeToIndexPath:(NSIndexPath *)newIndexPath fromIndexPath:(NSIndexPath *)oldIndexPath {
    	if (passwordViewController == nil) {
    		passwordViewController = [[PasswordViewController alloc] init];
    	}
    	// Set the detail controller's inspected item to the currently-selected item
    	passwordViewController.detailItem = [[detailItem objectForKey:@"accounts"] objectAtIndex:newIndexPath.row];
    	[[self navigationController] pushViewController:passwordViewController animated:YES];
    }
    
    @end
    Yes I know, a lot of the code looks familiar if you have looked at the SimpleDrillDown source code. :eek:
    :apple:
     
  9. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #9
    What about this code:

    Code:
    #import "MasterViewController.h"
    #import "UntitledAppDelegate.h"
    #import "UserNameViewController.h"
    
    @implementation MasterViewController
    
    @synthesize appController;
    @synthesize tableView;
    
    - (id)init
    {
    	if (self = [super init]) {
    		self.title = @"Categories";
    	}
    	return self;
    }
    
    - (void) inspectAction:(id) sender 
    {
    }
    
    - (void)loadView {
    	 UIButton *inspectButton = [UIButton buttonWithType:UIButtonTypeNavigation];
    	[inspectButton setTitle:@"A Button" forStates:UIControlStateNormal];
    	[inspectButton addTarget:self action:@selector(inspectAction:) forControlEvents:UIControlEventTouchUpInside];
    	self.navigationItem.customRightView = inspectButton;
    	[inspectButton release];
    
        tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    	tableView.delegate = self;
    	tableView.dataSource = self;
    	tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    	self.view = tableView;
    }
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    - (void)didReceiveMemoryWarning
    {
    	[super didReceiveMemoryWarning]; 
    }
    
    - (void)dealloc
    {
    		
    	tableView.dataSource = nil;
    	tableView.delegate = nil;
    	[tableView release];
    	[userNameViewController release];
    	[super dealloc];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	return [appController.masterList count];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withAvailableCell:(UITableViewCell *)availableCell
    {	
    	UISimpleTableViewCell *cell = nil;
    	if (availableCell != nil) {
    		// Use the existing cell if it's there
    		cell = (UISimpleTableViewCell *)availableCell;
    	} else {
    		cell = [[[UISimpleTableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	
    	// Get the object to display and set the value in the cell
    	NSDictionary *itemAtIndex = (NSDictionary *)[appController.masterList objectAtIndex:indexPath.row];
    	cell.text = [itemAtIndex objectForKey:@"title"];
    	return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView selectionDidChangeToIndexPath:(NSIndexPath *)newIndexPath fromIndexPath:(NSIndexPath *)oldIndexPath {
    
    	// Create the detail view lazily
    	if (userNameViewController == nil) {
    		userNameViewController = [[UserNameViewController alloc] init];
    	}
    	// Set the detail controller's inspected item to the currently-selected item
    	userNameViewController.detailItem = [appController.masterList objectAtIndex:newIndexPath.row];
    	[[self navigationController] pushViewController:userNameViewController animated:YES];
    }
    @end
    Yes there is a button called "A Button" that does nothing. I was just testing out navigation Items.
     
  10. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #10
    Thank you so much! It works.

    That taught me something about auto releasing variables :)

    *takes snapshot*
     
  11. macrumors newbie

    Joined:
    May 26, 2008
    #11
    May I ask, what works? I might be having a similar problem, but I don't see the post you seem to be responding to.
     
  12. macrumors regular

    Joined:
    Mar 27, 2008
    #12
    The OP was probably releasing property objects. If you are using properties and then you release them before dealloc() then it makes the object invalid and therefor you get this error.
     
  13. macrumors newbie

    Joined:
    May 26, 2008
    #13
    My problem is probably different, then. I create a navigation controller in my applicationDidFinishLaunching, setting the root view controller to my top view controller, which I initialize from a NIB. My top view controller has a butten that, when pressed, uses pushViewController to push another view controller onto the navigation controller's stack. This other view controller is also initialized from a (different) NIB.

    When I touch the back button from the other view controller, my application crashes. This is the last I see in the debugger:

    Code:
    #0	0x9302c6e8 in objc_msgSend
    #1	0x30b863cb in -[UIViewController view]
    #2	0x30b8b436 in -[UINavigationController _startTransition:fromViewController:toViewController:]
    #3	0x30b8bbd3 in -[UINavigationController _popViewControllerWithTransition:allowPoppingLast:]
    #4	0x30b8ba87 in -[UINavigationController popViewControllerAnimated:]
    #5	0x30b8c880 in -[UINavigationController navigationBar:shouldPopItem:]
    #6	0x30b48a22 in -[UINavigationBar popNavigationItemAnimated:]
    #7	0x30b4a251 in -[UINavigationBar _handleMouseUpAtPoint:]
    #8	0x30b2efbe in -[UIWindow sendEvent:]
    #9	0x30b1ec95 in -[UIApplication sendEvent:]
    #10	0x30b1e5c0 in _UIApplicationHandleEvent
    #11	0x3172d6a2 in SendEvent
    #12	0x3172f9ae in PurpleEventTimerCallBack
    #13	0x9678b62e in CFRunLoopRunSpecific
    #14	0x9678bd18 in CFRunLoopRunInMode
    #15	0x3172de5e in GSEventRunModal
    #16	0x3172df23 in GSEventRun
    #17	0x30b17482 in -[UIApplication _run]
    #18	0x30b21c86 in UIApplicationMain
    #19	0x000021c0 in main at main.m:13
    
     
  14. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #14
    I do not know where the post I was responding to went .. but whoever it was told me that when I created the button object [UIButton buttonWithType:], it was autoreleased. When I released it, it crashed. That was SDK version 1, this is 5, so things are different. However, the idea is the same: never release variables twice.
     
  15. macrumors regular

    Joined:
    Mar 27, 2008
    #15
    My new book by the Big Nerd Ranch guy stated that if you try to release an autorelease object you will get an error and cannot compile.

    Is this still an issue?
     
  16. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #16
    Unfortunately, it does compile. Meaning that if you have that mistake, you will not know it until later and it will not be as descriptive as an error message on a particular line.
     
  17. macrumors newbie

    Joined:
    May 26, 2008
    #17
    Boy do I feel dumb. I was using a local reference to my view, then releasing it! Don't try this at home:

    Code:
    - (void)viewDidLoad
    {
    	UIView *view = self.view;
    	.
    	.
    	.
    	[view release];
    }
    
     
  18. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #18
    Duke....

    How making nil and releasing differs???

    is it ok to do release like
    Code:
    [myObject release];
    myObject = nil;
    
    OR the following is enough???
    Code:
    myObject = nil;
    

    My app crashing sometimes on statup....

    Also i am getting mem-leaks for following line...

    Code:
    cell.image = [UIImage imageWithData:[NSData dataFromURL:[NSURL urlWithString:[I]myImagePath[/I]]]];
    
    Help plz...
     
  19. macrumors 6502

    Joined:
    Apr 28, 2008
    #19
    Code:
    [myObject release];
    myObject = nil;
    
    Is perfectly fine, but you probably don't need to set the reference pointed to by myObject to nil unless that's how you tell elsewhere in your code whether the reference points to allocated memory or not. In general, it's not needed and unless you really need to know whether that pointer points to a valid reference or not, just be diligent about your releases as you can get in to trouble if myObject was allocated by an Apple API object and you've just set it to nil and then the API object tries to use it (for example, a UIView).

    Doing
    Code:
    myObject = nil;
    
    Is BAD! Do NOT do this. You are essentially orphaning your reference at that point, and it cannot be released later on.
     
  20. macrumors newbie

    Joined:
    Jul 12, 2008
    #20
    pick some apple demo code

    I highly recommend starting with some apple demo code and molding that into what you need your app to do. Do it one step at a time so you can see where your app crashes. It helped me especially through the early beta. It also helps you understand excellent design practices.

    --Derrek

    ------
    iScale: iPhone nutrition tracking made easy!
    http://www.allofzero.com/
     

Share This Page