开发者

Setting synthesized arrays causing memory leaks using nested arrays

开发者 https://www.devze.com 2022-12-31 14:19 出处:网络
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 wi

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 r开发者_运维问答elease];
[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?

0

精彩评论

暂无评论...
验证码 换一张
取 消