我有一个问题,我认为是内存泄漏,一段时间后导致我的应用程序变慢。我有一个分段的 uitableview,其中包含"电影导演"的部分,在他们的特定部分中有一排他们的电影。为此,我调用了一个数据对象(并将节标题传递给它)以返回该节的数据并填充节行。所以我在同一个视图(numberOfRowsInSection、cellForRowAtIndexPath 和 didSelectRowAtIndexPath)上多次调用该对象,每个部分都会发生这种情况。查看 Instruments,我相信泄漏来自 getDirectorsMovies:theDirector from Movies.m。谁能告诉我我在做什么导致这个泄漏。任何帮助将不胜感激,我已经为此工作了几个星期。下面是一些代码来显示我在做什么。
提前致谢!!!
//Movies.h
#import Foundation/Foundation.h
#import sqlite3.h
#import"Movie.h"
@interface Movies : NSObject {
}
- (NSMutableArray *) getDirectorsMovies:(NSString *)theDirector;
@end
//Movies.m //getDirectorsMovies:(NSString *)theDirector goes to the database, gets the directors movies, and returns them in an array
#import"Movies.h"
@implementation Movies
- (NSMutableArray *) getDirectorsMovies:(NSString *)theDirector
{
sqlite3 *database;
NSString *databaseName = @"Movies.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSMutableArray *theDirectorsMovies = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement ="select * from movies where lastname = ? order by lastname, movie";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [theDirector UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *aLastName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aDirector = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aMovie = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
Movie *movie = [[Movie alloc] initWithName:aMovie lastname:aLastName director:aDirector];
[theDirectorsMovies addObject:movie];
[movie release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return theDirectorsMovies;
[theDirectorsMovies release];
}
@end
//调用getDirectorsMovies:(NSString *)theDirector
MoviesAppDelegate *appDelegate = (MoviesAppDelegate *)[[UIApplication sharedApplication] delegate];
Director *director = (Director *)[appDelegate.director objectAtIndex:indexPath.section];//appDelegate.director IS A MSMutableArray defined in the AppDelegate
self.theMovies = nil;//THIS IS A MSMutableArray defined in the AppDelegate
Movies *directorMovies = [[Movies alloc] init];
self.theMovies = [directorMovies getDirectorMovies:director.lastname];
[directorMovies release];
Movie *movie = (Movie *)[theMovies objectAtIndex:indexPath.row];
//do whatever with the data
[movie release];
你有这个:
return theDirectorsMovies;
[theDirectorsMovies release];
在 return 语句之后什么都没有发生,所以你对
return [theDirectorsMovies autorelease];
你的内存泄漏就会消失。
这里有许多潜在的问题,查看 Movie 的 init 方法会很有用,因为这可能就是问题所在。你也应该在 NSMutableArray.
上有一个自动释放
在我的 sqlite 代码中,我通常在 sqlite3_finalize 调用之前有以下语句
sqlite3_clear_bindings(compiledStatement);
那次调用为我的代码解决了很多问题。
以上就是短码网小编为大家整理的《使用uitableview和sqlite的iphone内存泄漏》相关内容,希望大家喜欢。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!
《使用uitableview和sqlite的iphone内存泄漏》文档下载仅供参考学习,下载后请在24小时内删除。
转载注明出处:https://www.duanma.net/article/8eafe8f1030.html