简体   繁体   中英

Setting synthesized arrays causing memory leaks using nested arrays

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!

That code looks correct upon quick examination.

Show the dealloc of your controller class; where are you releasing the objects?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM