2012-08-24 49 views
0

我正在使用此代码从config.plist文件中获取书名。然而,我的记忆管理是有问题的。 '[字典发布]'完全打破了应用程序,它退出。iOS发布不能按预期工作

代码在'[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; 
} 
+0

'MRC'或'ARC'? – holex

+0

如果他使用ARC,编译器会抱怨'释放'。 – Mundi

回答

0

我发现似乎是更好的解决方案。这让iOS管理内存。

//---finds the path to the application's Documents directory--- 
- (NSString *) documentsPath { 
    NSLog(@"Start documentsPath"); 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    // NSLog(@"Found documentsPath 40"); 
    NSLog(@"End documentsPath"); 
    return documentsDir; 
} 

- (NSString *) configPath { 
    NSLog(@"Start configPath"); 
    NSString *plistFileNameConf = [[self documentsPath] stringByAppendingPathComponent:@"Config.plist"]; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistFileNameConf]) { 
     plistFileNameConf = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"]; 
    } 
    NSLog(@"plistFile: %@",plistFileNameConf); 
    NSLog(@"End configPath"); 
    return plistFileNameConf; 
} 

根据需要下面的调用上面的代码:

NSString *Choice; 
NSArray *properties; 
NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[self configPath]]; 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
if (!temp) { 
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
} 
Choice = [temp objectForKey:@"Choice"]; 
properties = [temp objectForKey:Choice]; 
0

如果dict是不是已经强大的性能,使它之一。然后,在分配给它时使用self.dict(并保留发布)。

3

你需要合理地分配你的数组:

bnames = [[NSArray alloc] initWithArray:[dict valueForKey:@"BookNames"]]; 

仔细检查你的字典返回正确的数据类型。

1

似乎没有要你怎么了分配的NSDictionary的方式(虽然你也可以使用[NSDictionary的dictionaryWithContentsOfFile:],并保存自己不必担心释放

无论哪种方式,我建议问题不在于[release],而可能是在发行版之前:

bnames = [dict valueForKey:@"BookNames"]; 

a)在哪里分配。我没有看到任何地方的分配或声明? b)你期望回到什么类型的价值?

给它一个断点,并确保你得到你所期望的或任何东西。