A summary of the day.

Good day.  Silly exc_bad_access bug resolved.  A macro in gdb to print the retainCount (mentioned in a previously posted link) is indeed very handy.  Spamming this through the code was also useful:

NSLog(@"%@ <nameoffunction>",self);   <--- (or with another %@ and adding parameters)

where nameoffunction and the code itself was in things like dealloc, viewWillAppear, viewDidLoad etc.  Thus when it crashed and I could see the memory address as it would be somewhere in the debug console, this along with breakpoints at the aforementioned retainCount macro (or just typing print (int)[object retainCount] ) helped greatly.

Also interesting how sometimes the retain count is not what I thought it should be.  For example a custom table cell is deallocated.  This table cell was created the normal way with dequeueReusableCellWithIdentifier and then had a property set to an object.  So something like:

CustomTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomTableCell"]; cell.anObject = (AnObject *)newObject;

Where newObject was obtained from Core Data with the fetchedResultsController returning a MSManagedObject. In this case you just cast it to the type of object you want, as above. You see I would have thought that my newObject/anObject had a retainCount of 2 so when it’s deallocating the custom cell having deallocated the tableView it was in I should do this:

-(void)dealloc {
  [anObject release];
  [super release];
}

however checking the retain count for the object showed it to already be 0 at that point. The retain count never went above 1, e.g. the initial creation of it inside the table view. Once the superfluous release was commented out the problem went away and there are still no memory links, so it’s definitely not a case of fixing a problem by creating another.

On the opposite side of the coin if I create a view it’s retain count is 1. If I assign that view to a view controller I would expect it to be 2. Commonly once it’s assigned you can release it to return the retainCount to 1. However it isn’t the case. When the object is pushed to a controller the retain count went up to 5 and then dropped to 4 when release later on. Something like (pseudo code ish as I’m sans mac right now):

NewViewController *newView = [[NewViewController alloc] initWithNibName:@"NewView"];
newView.anObject = (AnObject *)newObject;
[self.navigationController pushNewViewController:newView animated:YES];

I assume the retain count for the view increases as things are assigned to it’s member properties.

Anyway the app at this stage is now, to the best of my knowledge, bug free. That leaves me open to implement the final view needed; and also the most bitchy complicated dynamic one.

On another note I’m a complete Dragon Age whore. I am intrigued by the warning on the just released golem DLC which essentially says “This is fucking hard”. I hope so as the rest of it, although fun, has been piss easy.

This entry was posted in Games, Programming and tagged , , , , . Bookmark the permalink.