如果有人能帮我解决我的漏水问题,我将不胜感激。泄漏发生在: aImage、aCategory、aDescription、category和categories。我在dealloc中释放它们,但显然这还不够:
-(void) readListFromDatabase:(char *) sqlStatement {
// Setup some globals
databaseName = @"mydatabase.sql";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Setup the database object
sqlite3 *database;
// Init the categories Array
categories = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
// Create a new category object with the data from the database
category=[[Category alloc] initWithName:aImage category_name:aCategory description_text:aDescription];
// Add the category object to the categories Array
[categories addObject:category];
[category release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
- (void)dealloc {
[databaseName release];
[databasePath release];
[categories release];
[aImage release];
[aCategory release];
[aDescription release];
[category release];
[super dealloc];
}发布于 2010-06-05 15:46:40
如果多次调用该方法,则字符串将泄漏,因为您需要释放以前的值。您还会在dealloc中过度释放字符串,因为您从未保留过它们。你应该像这样写作业:
[aImage release];
aImage = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)] retain];这些字符串可能泄漏的唯一另一种方式是,如果您从线程调用此方法,并且您没有创建自动释放池。
如果从新线程调用该方法,则需要一个自动释放池:
- (void)myThreadFunction {
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
try {
// ...
[self readListFromDatabase:whatever];
// ...
} @finally {
[pool release];
}
}发布于 2010-06-05 17:23:09
您发布的方法是否在同一对象上被多次调用?如果是,则第一次调用的类别将泄漏,因为每次调用readListFromDatabase:时都会覆盖该类别。尝试:
// Init the categories Array
[categories release];
categories = [[NSMutableArray alloc] init];发布于 2010-06-05 18:15:51
当我在循环中包含aImage自动释放时,为什么应用程序终止(如果是aImage释放,它也终止)?
https://stackoverflow.com/questions/2979479
复制相似问题