Memory leak problem?
How i releasing the dataArray from following snippet,
+(NSMutableArray *)getData: (NSString *)dbPath
{
NSMutableArray *_dataArray = [[NSMutableArray alloc] init];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *开发者_Go百科sqlQuery = [NSString stringWithFormat:@"SELECT DISTINCT name FROM databaseTable"];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, [sqlQuery UTF8String], -1, &selectstmt, NULL) == SQLITE_OK)
{
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
[_dataArray addObject:[NSString stringWithFormat:@"%d", sqlite3_column_int(selectstmt, 0)]];
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
return _dataArray;
}
The above method gives me memory leak and it getting me serious problem in future working of application.
return [_dataArray autorelease];
If you want to returned retained objects you need to make that clear by following the naming conventions. the method should start with new, create or copy. Otherwise you should return a autoreleased object.
I don't see any obvious leaks in the code you posted. The function is returning a NSMutableArray that has been allocated, so the caller would be responsible for calling release
at some later point. Or, you may choose to make this an autorelease
object.
Also, you probably want to call sqlite3_close() only if sqlite3_open() succeeded (i.e., move sqlite3_close() to inside the first if
statement). Same idea for sqlite3_finalize().
There is some one-time initialization that SQLite does implicitly, but you shouldn't need to worry about that. Check the docs for:
int sqlite3_initialize(void);
int sqlite3_shutdown(void);
What type of object(s) are being reported as leaks?
精彩评论