Object Mysteriously Losing Properties

Discussion in 'iOS Programming' started by Lakario, Nov 4, 2008.

  1. Lakario macrumors member

    Oct 29, 2008
    I am not exactly sure what code to post with this considering the nature of what's going on, so I'll do my best to explain and then perhaps I can provide code based on your responses.

    When my application loads in the AppDelegate I create 3 objects of the "Item" type and then create an array out of those objects and add that array to an ItemListController which has a property for an NSMutableArray. The program then loads a UINavigationController with each of those objects in it being drawn with a subClass of UITableViewCell. As it stands now, if I click on any one of those objects more than 3 times, the object itself does not become nil, but all of the fields within it do.

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    	Item *sword = [[Item alloc] initWithName:@"Sword" description:@"This is a sword"];
    	//sword.itemPrice = (int *)524;
    	Item *shield = [[Item alloc] initWithName:@"Shield" description:@"This is a shield"];
    	//shield.itemPrice = (int *)322;
    	Item *subligar = [[Item alloc] initWithName:@"Subligar" description:@"This is a subligar"];
    	//subligar.itemPrice = (int *)1034;
    	[subligar setImageWithPath:@"Hello.png"];
    	if(self.itemListController == nil) {
    		ItemListController *controller = [[ItemListController alloc] init];
    		self.itemListController = controller;
    		[controller release];
    	NSMutableArray *itemArray = [[NSMutableArray alloc] initWithObjects:sword, shield, subligar, nil];
    	self.itemListController.items = itemArray;
    	[itemArray release];
    	// Configure and show the window
    	[window addSubview:[navigationController view]];
    	[window makeKeyAndVisible];
    The click handler of my TableView right now simply calls the AppDelegate's instance of ItemListController and then gets the object back from the items array at the index at which the user clicked. From there, I set a few properties and load a view tailored to that Item.

    Presently, all of the logic that sets properties with the Item and loads the view for that object is commented out and so the only behavior occurring is the Item being retrieved from the AppDelegate's ItemListController.items and then being stored in a temporary object which is later released. But like I said above, after 3 requests for my object, all of its fields become invalid. I have no idea what is causing it because the object itself is never being released within that time. Below is the code from the click event of the table cell:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	NSLog(@"Hit Count: %d", calls);
    	iPhoneHelloWorldAppDelegate *appDelegate = (iPhoneHelloWorldAppDelegate *)[[UIApplication sharedApplication] delegate];
    	Item *item = (Item *)[appDelegate.itemListController.items objectAtIndex:indexPath.row];
    	//lastItem = item;
    	if(self.itemView == nil) {
    		ItemViewController *viewController = [[ItemViewController alloc] initWithNibName:@"ItemViewController" bundle:[NSBundle mainBundle]];
    		self.itemView = viewController;
    		[viewController release];
    	if(calls > 3) {
    		NSLog(@"item retainCount = %d", [item retainCount]);
    	[item release];
    Here's a snapshot from the debugger after my Item object dies:
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Can you explain the purpose of this line

    	[item release];
  3. Lakario thread starter macrumors member

    Oct 29, 2008
    Releasing the local object that is a reference to the Item object that exists in the AppDelegate. Should I not do that?
  4. Lakario thread starter macrumors member

    Oct 29, 2008
    Ugh. Apparently I shouldn't have. I removed that and no more crash. How is it that that reference is forcing the base object to be destroyed and why does it happen only after the 3rd call?
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You just need to follow the memory rules. Getting an object from an array doesn't transfer ownership. You didn't retain it so don't release it.

Share This Page