Ankit Sachan
Ankit Sachan

Reputation: 7840

iPhone: Strange leak

Ok the below problem is solved but now its creating leak for this block

NSMutableArray *tempRowArray=[[NSMutableArray alloc] init];
    [tempRowArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
    [pickerList addObject:tempRowArray];
    [tempRowArray release];

for tempRowArray

ahh I am lost in this memory problems.

Solved: Hi,

Can you please help me in this, I have following condition

1) Declared an array in .h file.
2) Created property and synthesized it
3) In viewWillAppear allocated memory to it

pickerList = [[NSMutableArray alloc] init];

4) Read data from database and stored objects in this array

if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

    while (sqlite3_step(compiledStatement) == SQLITE_ROW) {

        NSMutableArray *tempRowArray=[[NSMutableArray alloc] init];
        [tempRowArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
        [pickerList addObject:tempRowArray];
        [tempRowArray release];
    }
}

picker list is shown as memory leak out there and application eventually crashes after some time please help me get rid of it

5) I have released pickerList in dealloc() and set it to nil in viewDidUnload

Upvotes: 0

Views: 170

Answers (3)

Abizern
Abizern

Reputation: 150605

Use the synthesized setter method to set the array in viewDidLoad

self.pickerList=[NSMutableArray array];

And set it to nil in viewDidUnload

self.pickerList = nil;

The reason you are getting a leak is because you are probably calling pickerList = nil in viewDidUnload

There is a difference between pickerList = something and self.pickerList = something. In the first instance you are setting the value directly, but if you use the second version (and you have declared @property (retain) NSMutableArray *pickerList the @synthesized setter method will retain the new value and release the old value.

In your case, by just setting it to nil in viewDidUnload you lose the pointer to the original object, so you can't release it, and you have a leak.

Upvotes: 2

user745098
user745098

Reputation:

In viewWillAppear you are allocating memory. In case of you are switching to and from this view controller, your viewWillAppear will be getting called and you would be allocating memory again and again.

Do it in viewDidLoad and also use its property while allocating it.

Upvotes: 2

PengOne
PengOne

Reputation: 48398

Every time the view appears you allocate pickerList. Allocate it in viewDidLoad and then release it in viewDidUnload.

Upvotes: 4

Related Questions