Jules
Jules

Reputation: 7766

Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1

I'm getting a memory leak when I`m trying to reset a table view, I thought I could just put release before, but this doesn't help.

What do I need to do here ?

-(void) resetTable{

    recordOffset = 10;
    rOFactor = 0;
    booShowMoreCell = false; 
    self.transactionsArray = [[NSMutableArray alloc] init]; // leak here
}

Upvotes: 0

Views: 3266

Answers (3)

Jonathan.
Jonathan.

Reputation: 55594

Assuming that transactionsArray is a retained property, the problem you are having is that the NSMutableArray is being retained twice.

When you set a retained property it releases the old value, and retains the new (incoming) value. The alloc method also retains the object.

So

//   \/--- retain count = 2               \/-- retain count = 1
self.transactionsArray = [[NSMutableArray alloc] init];

The shortest way to resolve this is autorelease the NSMutableArray:

self.transactionsArray = [[[NSMutableArray alloc] init] autorelease];

There's a convenience method way for the above line:

self.transactionsArray = [NSMutableArray array];

Upvotes: 6

Paul
Paul

Reputation: 1303

On the first sight I would say that you just overwrite the pointer with a reference to a new object and forgetting the old reference which points to an object that was not released yet. As long as you do not use ARC you should use

[self.transactionsArray release]; // Be sure it was alloced before
self.transactionsArray = [[NSMutableArray alloc] init];

Upvotes: 0

Emil
Emil

Reputation: 7256

As I can't see a [tableView release]-call in this code, that's what's missing - you aren't releasing the tableView before allocating a new one on top of it. That means you're adding a retain count, without removing one first. Adding the release will fix the leak.

Upvotes: 0

Related Questions