Setting synthesized arrays causing memory leaks using nested arrays

Posted by webtoad on Stack Overflow See other posts from Stack Overflow or by webtoad
Published on 2010-05-19T22:24:19Z Indexed on 2010/05/19 22:30 UTC
Read the original article Hit count: 229

Hello:

Why is the following code causing a memory leak in an iPhone App?

All of the initted objects below leak, including the arrays, the strings and the numbers. So, I'm thinking it has something to do with the the synthesized array property not releasing the object when I set the property again on the second and subsequent time this piece of code is called.

Here is the code:

"controller" (below) is my custom view controller class, which I have a reference to, and I am setting with this code snippet:

sqlite3_stmt *statement;
NSMutableArray *foo_IDs = [[NSMutableArray alloc] init];
NSMutableArray *foo_Names = [[NSMutableArray alloc] init];
NSMutableArray *foo_IDsBySection = [[NSMutableArray alloc] init];
NSMutableArray *foo_NamesBySection = [[NSMutableArray alloc] init];

// Get data:
NSString *sql = @"select distinct p.foo_ID, p.foo_Name from foo as p ";
if (sqlite3_prepare_v2(...) == SQLITE_OK) {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        int p_id;
        NSString *foo_Name;

        p_id = sqlite3_column_int(statement, 0);
        char *str2 = (char *)sqlite3_column_text(statement, 1);
        foo_Name = [NSString stringWithCString:str2];

        [foo_IDs addObject:[NSNumber numberWithInt:p_id]];
        [foo_Names addObject:foo_Name];
    }
    sqlite3_finalize(statement);
}

// Pass the array itself into another array:
// (normally there is more than one array in each array)
[foo_IDsBySection addObject: foo_IDs];
[foo_NamesBySection addObject: foo_Names];

[foo_IDs release];
[foo_Names release];

// Set some synthesized properties (of type NSArray, nonatomic, 
// retain) in controller:
controller.foo_IDsBySection = foo_IDsBySection;
controller.foo_NamesBySection = foo_NamesBySection;

[foo_IDsBySection release];
[foo_NamesBySection release];

Thanks for any help!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about memory-leak