我仍然堆叠着一个简单的objC代码,它从plist中检索一些数据。在我使用它们之后,我无法释放对象,因为它失败了......
- (void)retrieveFromPlist:(NSString*)Nazov
{
NSLog(@"Objekt: %@",Nazov);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"working.plist"];
////Zober vsetky ulozene
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
subDict = [dict objectForKey:Nazov];
NSString *hudbaR;
float hudbaL;
NSString *zvukR;
float zvukL;
hudbaL = [[subDict objectForKey:@"musicLevel"] floatValue];
hudbaR = [subDict objectForKey:@"musicRow"];
zvukL = [[subDict objectForKey:@"soundLevel"] floatValue];
zvukR = [subDict objectForKey:@"soundRow"];
NSLog(@"Musical level: %f, musical roww:%@ , zuk level: %f, zuk row: %@", hudbaL,hudbaR ,zvukL ,zvukR );
if (hudbaR) {
[musicController setBackgroundSoundVolume:zvukL];
[musicController setBackgroundMusicVolume:hudbaL];
MusicsliderCtl.value = hudbaL;
sliderCtl.value = zvukL;
[musicController playMusicWithKey:hudbaR timesToRepeat:0];
[musicController playSoundWithKey:zvukR timesToRepeat:0];
}
//[dict release];
//[subDict release];
}
答案 0 :(得分:2)
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
subDict = [dict objectForKey:Nazov];
第二个语句会覆盖已分配的subDict。这会导致内存泄漏。然后,因为您不拥有[dict objectForKey:Nazov]
,-release
会导致解除分配错误。
你可以写
NSDictionary* subDict = [dict objectForKey:Nazov];
并且不要-release
因为您不是所有者。 ([dict release]
仍然需要+alloc
,因为你是{{1}}的人。)
如果您没有更改集合,请选择immutable(NSDictionary)over mutable(NSMutableDictionary)。
答案 1 :(得分:0)
您正试图释放您的代码没有所有权的自动释放对象(subDict):
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
subDict = [dict objectForKey:Nazov];
原始subDict
变量被objectForKey:
消息的返回值覆盖。导致首先泄漏原始对象,然后在自动释放池尝试释放新对象时崩溃。
要更正此问题,请删除代码中的以下行:
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
...
[subDict release];