Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Lakario

macrumors member
Original poster
Oct 29, 2008
30
0
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.

Code:
- (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:

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	calls++;
	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:
picture2az5.png
 

Lakario

macrumors member
Original poster
Oct 29, 2008
30
0
Releasing the local object that is a reference to the Item object that exists in the AppDelegate. Should I not do that?

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?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.