开发者

cannot release objects after retrieving from plist

开发者 https://www.devze.com 2023-01-06 18:54 出处:网络
I am still stacked with a simple objC code which retrieve some data from plist. After I used them I cannot release object because it fails...

I am still stacked with a simple objC code which retrieve some data from plist. After I used them I cannot release object because it fails...

- (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:开发者_如何学Go@"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];
}


NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
subDict = [dict objectForKey:Nazov];

The 2nd statement overrides the already allocated subDict. This causes memory leak. Then because you don't own the [dict objectForKey:Nazov], -releaseing it causes deallocation error.

 

You could just write

NSDictionary* subDict = [dict objectForKey:Nazov];

and don't -release it since you are not the owner. ([dict release] is still needed as you are the one who +alloc it.)

If you're not changing the collection, prefer immutable (NSDictionary) over mutable (NSMutableDictionary).


You are trying to release an autoreleased object (subDict) that your code doesn't have ownership over:

NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
subDict = [dict objectForKey:Nazov];

The originally subDict variable is being overwritten by the return value from the objectForKey: message. Resulting in first a leak of the original object and then a crash when the autorelease pool tries to release the new object.

In order to correct this, remove the following lines in your code:

NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
...
[subDict release];
0

精彩评论

暂无评论...
验证码 换一张
取 消