Meroon
Meroon

Reputation: 3488

iPhone memory leak seems to be out of my control

So I'm making a very basic Twitter app (it's actually Presence 2 from the Stanford iPhone course that's on iTunes), when I decided I wanted to see if my application was leaking. So I ran Leaks and it found one right off the bat. But when I look at the stack trace the leak appears to happen in the main function when I call UIApplicationMain.

The image below shows the stack trace from instruments and the corresponding code in xcode. Does anyone know how I can stop this leak and why it's happening?

alt text http://img193.imageshack.us/img193/1237/picture2fnj.png

EDIT: Ok I've searched and searched and got to where the problem is, but I still don't know what's going on. I've included the source for the TableViewController I'm having problems with.

The leak happens when I set the cell.text to [names objectAtIndex:indexPath.row]. Whats interesting is that it's NSIndexPath that appears to be leaking somehow. How should I be managing memory with the objectAtIndex method?

On an unrelated topic, is editing my question post the best way to reply? Or should I have posted my code in a comment?

@implementation PersonListTableViewController


- (id)initWithStyle:(UITableViewStyle)style 
{
    if (self = [super initWithStyle:style])
    {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TwitterUsers" ofType:@"plist"];  
    names = [[NSArray alloc] initWithContentsOfFile:path];
    }
    return self;
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.text = [names objectAtIndex:indexPath.row];

    return cell;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryDisclosureIndicator;
}


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


@end

Upvotes: 0

Views: 1861

Answers (3)

Nosredna
Nosredna

Reputation: 86326

Are you running the tools on the device or the simulator? I've found that memory issues can be different between the two.

Upvotes: 1

Frank V
Frank V

Reputation: 25429

Start with the basics. Make sure that you are releasing all of your UI controls and business objects in the first object that is loaded. Make sure all resources during initalizition is properly released and then ensure you are instating things correctly. Are you using the pattern like?:

UIButton *btn = [[UIButton alloc] init]; //not really complete...
myObj.myButton = btn;
[btn release]

Finnally if nothing else step though the code in your mind and identify when something is being alloc and then identify the exact point that it is being released. If you can't identify where the release is, you've probably found your memory leak. Find a solution and retest. It make take a while to identify every leak. I always assume that there are multiple sources until they or it is resolved.

Like Andy mentions, if you need more direct help you are going to need to post more code.

Upvotes: 0

Andy White
Andy White

Reputation: 88475

There is something in your application code that is leaking. The UIApplicationMain function is the entry point of the stack trace, if you follow the calls down into your application code, that is where you will find the leak.

If you can identify the method that seems to be leaking, you can post that code, and someone might be able to help more.

Upvotes: 0

Related Questions