我是iPhone开发的新手,我有这个内存泄漏问题。
我正在使用NSMutableArray检索位于Documents目录中的.plist文件的内容。
第一次使用它时,一切正常,但如果我多次调用它,就会出现内存泄漏。
这是我的代码:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
fullFileName = [NSString stringWithFormat:@"%@/SavedArray", documentsDirectory];
//retrieve your array by using initWithContentsOfFile while passing
//the name of the file where you saved the array contents.
savedArray = nil;
savedArray = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
self.composedArray = [savedArray copy];
[savedArray release];
[self.tableView reloadData];
}每次视图消失时,我都会释放它
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[composedArray release];
composedArray = nil;
[savedArray release];
}我使用的是Instruments,这显示了内存泄漏的来源是下面这行代码:
savedArray = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];我不知道如何解决这个漏洞,如果有人能分享任何解决方案,我将非常感谢。
提前谢谢。
发布于 2010-01-19 05:54:58
composedArray属性的声明如何
如果声明为:
@property(retain) id composedArray;这就是内存泄漏的地方。copy增加引用计数,retain也增加引用计数。如果任何时候你赋值给composedArray,你将赋值一个副本(通过阅读你的代码看起来),你应该声明你的属性为:
@property(copy) id composedArray;然后更改您的代码以执行以下操作:
self.composedArray = savedArray;(复制将在合成访问器中进行)。
https://stackoverflow.com/questions/2088795
复制相似问题