3
votes

APPLICATION DESCRIPTION: I am a new iPhone developer. I am working on an application which used two table views then a detail view. The first level table view presents a few categories, when a category is selected a list of topics related to that category is displayed. When a topic is selected a detail view is presented related to that topic.

When I run the app in the simulator everything seems to work fine (but only briefly!):

  • First Level Table View: this view with the categories loads fine and works without any problems.
  • Topic List View: This view also appears to work fine, but only briefly. It loads all of the correct topics, and I can even quickly scroll up and down through the entire list of topics - but this only works for a couple of seconds while scrolling (after which the application crashes). I can also select any of the topics and the detail view loads successfully.
  • Detail View: works fine, no problems.

I am using two nibs for the application: MainWindow.xib displays the First Level and Topic View tables. A TopicDetail.xib is used to display the detail information for a topic.

DATA: The First Level View gets its data from a plist which consists of an array of strings. The Topic List View gets its data from a plist of dictionaries. Each dictionary consists of five strings. One of these strings contains "Tags" of categories which relate to the topic. When a category from the First Level View is selected, the Topic View selects the topics with "Tags" which match the selected category, and displays them on the Topic View Table.

PROBLEM: My problem is with the second "Topic View." A couple of ways in which I have been able to cause the crash:

  • If I quickly scroll up and down through the topic list, for a couple of seconds everything works normally, then the application crashes.

  • If I scroll one cell off of the screen, I can scroll the same cell back onto the screen without a problem. If I repeat this (scroll the same cell off of the screen a second time then try to scroll it back onto the screen), the app crashes when scrolling the cell back onto the screen.

ERROR MESSAGES: The initial error message that was returned was EXC_BAD_ACCESS. When I enabled NSZombieEnabled I got the error message "-[CFString retain]: message sent to deallocated instance"

Based on these error messages and my research, I believe I have a memory allocation problem where "retain" is being called on an object that has been released, but I can't figure out where this occuring and how to resolve it! Any help would be greatly appreciated - thank you in advance!!

CODE:

First Level View Controller.h:

#import 
@class TopicListController;

@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
    TopicListController *childController;
}
@property (nonatomic, retain) NSArray *controllers;
@end

First Level View Controller.m:


#import "FirstLevelViewController.h"
#import "TopicListController.h"
#import "TopicAppDelegate.h"

@implementation FirstLevelViewController
@synthesize controllers;

- (void)viewDidLoad
{
    self.title = @"Categories"; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TopicCategoryList" ofType:@"plist"];  
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];   
    self.controllers = array;
    [array release];
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    self.controllers = nil;
    [childController release];
    childController = nil;
    [super viewDidUnload];
}

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

#pragma mark -
#pragma mark Table Data Source Methods

- (NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section
{
    return [self.controllers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *FirstLevelCell = @"FirstLevelCell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [controllers objectAtIndex:row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (childController == nil) {
        childController = [[TopicListController alloc] init];
    }

    childController.title = @"Topics";
    NSUInteger row = [indexPath row];

    NSString *selectedCategory = [self.controllers objectAtIndex:row];
    childController.selectedCategory = selectedCategory;
    [self.navigationController pushViewController:childController animated:YES];
}                            

@end

Topic List Controller.h:

#import 
@class TopicDetailController;

@interface TopicListController : UITableViewController {

    NSArray *list;
    NSString *selectedCategory;
    TopicDetailController *childController;

}

@property (nonatomic, retain) NSArray *list;
@property (nonatomic, retain) NSString *selectedCategory;

@end

Topic List Controller.m

#import "TopicListController.h"
#import "TopicAppDelegate.h"
#import "TopicDetailController.h"
#import "NSArray-MutableDeepCopy.h"
#import "TopicConstants.h"

@implementation TopicListController

@synthesize list;
@synthesize selectedCategory;

- (void)viewWillAppear:(BOOL)animated
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TopicContent" ofType:@"plist"];

    NSMutableArray *fullArray = [[NSMutableArray alloc] initWithContentsOfFile:path];   
    NSMutableArray *fullArrayCopy = [fullArray mutableDeepCopy];

    NSUInteger items = [fullArrayCopy count];
    NSUInteger item=0;

        for (item; item /*less than*/ items; item++) {
            if ([[[fullArrayCopy objectAtIndex:item] objectForKey:CATEGORIES_KEY] 
             rangeOfString:selectedCategory 
             options:NSCaseInsensitiveSearch].location == NSNotFound) 
        {
            [fullArrayCopy removeObjectAtIndex:item];
            item--;
            items--;
        }
    }

    self.list = fullArrayCopy;
    [fullArray release];
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}

- (void)viewDidUnload
{
    self.list = nil;
    self.selectedCategory = nil;
    [childController release];
    childController = nil;
}

- (void)dealloc
{
    [list release];
    [selectedCategory release];
    [childController release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
{
    return [list count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *DisclosureButtonCellIdentifier = @"DisclosureButtonCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DisclosureButtonCellIdentifier];

    if (cell==nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:DisclosureButtonCellIdentifier]
                autorelease];
    }

    NSUInteger row = [indexPath row];
    NSString *rowString = [[list objectAtIndex:row] objectForKey:TITLE_KEY];
    NSString *rowDetailString = [[list objectAtIndex:row] objectForKey:SUBTITLE_KEY];
    cell.textLabel.text = rowString;
    cell.detailTextLabel.text = rowDetailString;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    [rowString release];
    [rowDetailString release];
    return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (childController == nil) {
        childController = [[TopicDetailController alloc] initWithNibName:@"TopicDetail" bundle:nil];
    }


    NSUInteger row = [indexPath row];

    NSString *selectedTopicInformation = [[list objectAtIndex:row] objectForKey:INFORMATION_KEY];
    NSString *selectedTopicTitle = [[list objectAtIndex:row] objectForKey:TITLE_KEY];
    NSString *selectedTopicSubtitle = [[list objectAtIndex:row] objectForKey:SUBTITLE_KEY];
    NSString *selectedTopicTips = [[list objectAtIndex:row] objectForKey:TIPS_KEY];

    NSString *detailMessageInformation  = [[NSString alloc] initWithFormat:@"%@.", selectedTopicInformation];
    NSString *detailMessageTitle = [[NSString alloc] initWithFormat:@"%@", selectedTopicTitle];
    NSString *detailMessageSubtitle = [[NSString alloc] initWithFormat:@"%@", selectedTopicSubtitle];
    NSString *detailMessageTips = [[NSString alloc] initWithFormat:@"%@", selectedTopicTips];

    childController.messageInformation = detailMessageInformation;
    childController.messageTitle = detailMessageTitle;
    childController.messageSubTitle = detailMessageSubtitle;
    childController.messageTips = detailMessageTips;

    childController.title = @"Topic Detail";

    [detailMessageInformation release];
    [detailMessageTitle release];
    [detailMessageSubtitle release];
    [detailMessageTips release];
    [self.navigationController pushViewController:childController animated:YES];


}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if (childController == nil) {
        childController = [[TopicDetailController alloc] initWithNibName:@"TopicDetail" bundle:nil];
    }

    NSUInteger row = [indexPath row];

    NSString *selectedTopicInformation = [[list objectAtIndex:row] objectForKey:INFORMATION_KEY];
    NSString *selectedTopicTitle = [[list objectAtIndex:row] objectForKey:TITLE_KEY];
    NSString *selectedTopicSubtitle = [[list objectAtIndex:row] objectForKey:SUBTITLE_KEY];
    NSString *selectedTopicTips = [[list objectAtIndex:row] objectForKey:TIPS_KEY];

    NSString *detailMessageInformation  = [[NSString alloc] initWithFormat:@"%@.", selectedTopicInformation];
    NSString *detailMessageTitle = [[NSString alloc] initWithFormat:@"%@", selectedTopicTitle];
    NSString *detailMessageSubtitle = [[NSString alloc] initWithFormat:@"%@", selectedTopicSubtitle];
    NSString *detailMessageTips = [[NSString alloc] initWithFormat:@"%@", selectedTopicTips];

    childController.messageInformation = detailMessageInformation;
    childController.messageTitle = detailMessageTitle;
    childController.messageSubTitle = detailMessageSubtitle;
    childController.messageTips = detailMessageTips;

    childController.title = @"Topic Detail";

    [detailMessageInformation release];
    [detailMessageTitle release];
    [detailMessageSubtitle release];
    [detailMessageTips release];
    [self.navigationController pushViewController:childController animated:YES];
}

@end
1

1 Answers

10
votes

Your problem is in tableView:cellForRowAtIndexPath:. You shouldn't release the following:

[rowString release];
[rowDetailString release];

when you get the string like:

NSString *rowString = [[list objectAtIndex:row] objectForKey:TITLE_KEY];
NSString *rowDetailString = [[list objectAtIndex:row] objectForKey:SUBTITLE_KEY];

They are returned as autoreleased objects. You should only ever release something you own, i.e. something you have alloced, retained or gotten from a method starting with new. A good article explaining this is objective-c memory management for lazy people. But to quote the important part:

  • You own it if you alloc it.
  • You own it if you copy it.
  • You own it if you new it. (New is simply a shortcut for alloc/init).