【发布时间】:2012-08-24 11:04:21
【问题描述】:
我正在使用此代码从 config.plist 文件中获取书名。但是我的内存管理是有问题的。 '[dict release]' 完全破坏了应用程序并退出。
当 '[dict release]' 被删除时,代码可以工作,但据我所知,它会导致内存泄漏。
bnames 是一个全局 NSMutableArray
我做错了什么?
- (NSString *)loadBookname: (NSInteger) bookToLoad {
bookToLoad = [self bookOrder:bookToLoad];
//---get the path to the property list file---
plistFileNameConf = [[self documentsPath] stringByAppendingPathComponent:@"Config.plist"];
//---if the property list file can be found---
if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileNameConf]) {
//---load the content of the property list file into a NSDictionary object---
dict = [[NSDictionary alloc] initWithContentsOfFile:plistFileNameConf];
bnames = [dict valueForKey:@"BookNames"];
[dict release];
}
else {
//---load the property list from the Resources folder---
NSString *pListPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
dict = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
bnames = [dict valueForKey:@"BookNames"];
[dict release];
}
plistFileNameConf = nil;
NSString *bookNameTemp;
bookNameTemp = [bnames objectAtIndex:bookToLoad - 1];
NSLog(@"bookName: %@", bookNameTemp);
return bookNameTemp;
}
【问题讨论】:
-
如果他使用ARC,编译器会抱怨
release。
标签: ios memory memory-management memory-leaks