0
我試圖從我的代碼中刪除內存泄漏有一些麻煩。在下面的代碼中,我得到了一行內存泄漏「configurationArray = [[NSArray arrayWithContentsOfFile:controllerConfigurationFilePath] retain];」但是,當我刪除保留,應用程序崩潰,並將保留更改爲autorelease也會導致崩潰。內存泄漏與陣列 - 目標c
感謝, 威廉
-(NSArray*)decodeConfigurationFile:(NSString*)fileName{
NSArray* configurationArray = [[NSArray alloc] init];
NSString *controllerConfigurationFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
if (controllerConfigurationFilePath != nil) {
// returns array of items storing the data for form
configurationArray = [[NSArray arrayWithContentsOfFile:controllerConfigurationFilePath] retain];
}
// returns fields dictionary objects from plist into an array
return [[configurationArray objectAtIndex:0] objectForKey:@"fields"];
}
感謝您的快速回復。我實現了上述更改。並確保返回的數組分配給的變量(「字段」)具有保留。然而,在我的代碼中,這個變量被擦除,當我嘗試使用它時,它崩潰了。我認爲這可能與不保留這個「[NSArray arrayWithContentsOfFile:controllerConfigurationFilePath]」然後試圖用這個語句來使用它:「[[field objectAtIndex:index] objectForKey:@」costAtHour「];」 – williamb 2010-09-03 15:45:23
進一步調查,顯示我在訪問該屬性時未使用self.fields,這是導致崩潰的原因。 謝謝 – williamb 2010-09-03 15:59:28
它可能有助於查看調用隱含的'fields'字典的.plist條目。 – falconcreek 2010-09-03 16:08:31