我有以下代码,它显示了在stringWithUTF8String语句附近的对象收藏夹的内存泄漏。
我已在该属性中声明了收藏
-(NSMutableArray *) readFavoritesFromDatabase
{
// Check if database is present
[self setDatabaseNameAndPath];
[self checkAndCreateDatabase];
// Setup the database object
sqlite3 *database;
//Initialize favorites array
if (favorites == nil)
{
[favorites release];
favorites = [[NSMutableArray alloc] init];
}
else
{
favorites = nil;
[favorites removeAllObjects];
}
// Open the database from the users file system
if(sqlite3_open([self.dataBasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from Favorites";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the favorites array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Create Favorite object and add it to the Favorite array
Favorite *favorite = [[[Favorite alloc] init] autorelease];
favorite.cameraID = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 0)];
favorite.cameraName = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 1)];
favorite.cameraLink = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(compiledStatement, 2)];
[self.favorites addObject:favorite];
//[favorite.cameraID release];
// [favorite.cameraName release];
// [favorite.cameraLink release];
}
// If favorite cameras exists in database, then sort the Favorites array
if([self.favorites count]>0)
{
NSSortDescriptor *favoritesNameSorter = [[NSSortDescriptor alloc] initWithKey:@"cameraName" ascending:YES];
[self.favorites sortUsingDescriptors:[NSArray arrayWithObject:favoritesNameSorter]];
[favoritesNameSorter release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
// Close the database
if(database !=nil)
{
sqlite3_close(database);
return self.favorites;
}
else
{
return nil;
}
}请提前告诉我如何解决这个内存泄漏问题,谢谢。
发布于 2010-09-06 15:27:39
使用此安全方法:
Favorite *tempFavorite = [[Favorite alloc] init];
self.favorite = tempFavorite;
[tempFavorite release];通常,在你最喜欢的dealloc函数中,在调用超级dealloc函数之前,你应该移除所有的对象并清理必要的东西。
使用这种方式,您不需要担心favorite是否为nil,因为objective-c允许调用nil对象的方法
问候
梅尔·阿萨亚格
发布于 2010-09-06 15:29:02
不确定stringWithUTF8String泄漏,但这是一个问题:
favorites = nil;
[favorites removeAllObjects];您泄漏了favorites中的内容,然后告诉nil对象删除所有对象--它是空的,根据定义它没有任何对象。然后,您尝试向其中添加对象;这也不会起作用。
发布于 2010-09-06 14:42:13
我没有看到你的stringWithUTF8String中有任何漏洞,代码运行得很好。然而,看看整个方法,我发现一些东西会造成内存问题,比如泄漏或崩溃。如果您已经为favorites声明了一个属性,那么在这里应该使用self.favorites
//Initialize favorites array
if (favorites == nil)
{
[favorites release];
favorites = [[NSMutableArray alloc] init];
}
else
{
favorites = nil;
[favorites removeAllObjects];
}变成:
//Initialize favorites array
if (self.favorites == nil)
{
self.favorites = [[NSMutableArray alloc] init];
}
else
{
self.favorites = nil;
}它将帮助你在内存管理中做很多事情,就像在else条件下,你将变量设置为nil,但没有释放它,在第一个条件中,你释放了一个nil对象?
https://stackoverflow.com/questions/3649472
复制相似问题