开发者

Clear a NSDictionary of NSarrays

开发者 https://www.devze.com 2023-04-01 13:40 出处:网络
I spent a lot of time trying hundreds of things, and searching on the internet for my problem, but I didn\'t find anythig, so I hope you guys will be able to help me :).

I spent a lot of time trying hundreds of things, and searching on the internet for my problem, but I didn't find anythig, so I hope you guys will be able to help me :).

So, I have an NSMutableDictionary that I populate with NSArrays when I parse an XML document. My problem is, I want to empty it before I release it because it appears to cause leaks !

Best solution, to my eyes, is : [myDict removeObjectsForKeys : [myDict allKeys]] but the app crashes and I get the following message in the console :

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

I really can't figure why... any ideas ?

EDIT

Important point I forgot (I guess...), is that the dictionary I'm calling is in my application Delegate, with appDelegate = [[UIApplication sharedApplication] delegate];. (appDelegate defined in my .h with myAppDelegate class type).

Then, I initialize it like this : appDelegate.myDict = [[NS开发者_运维技巧MutableDictionary alloc] init];

Is it wrong ?

What's weird then, is that when I do :

    [appDelegate.myDict release];
    NSLog( @"%@", appDelegate.myDict);

I get the dictionary filled...

EDIT 2

I just figured out that all my appDelegate variables are not released when I call [appDelegate.myVariable release] ... Wtf am I doing wrong ? This is driving me crazy ><


When you release a dictionary or array it releases all the elements inside it. If you're getting leaks they're not due to a failure to remove the objects.

Of course, in your case part of your problem appears to be that you don't know where the error is occurring. -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0) indicates that you were executing objectAtIndex on an (empty) NSArray, and that doesn't happen (directly) if you do [myDict removeObjectsForKeys : [myDict allKeys]] on an NSDictionary. So you need to figure out where that error is really coming from.


What about this:

// myDict should be NSMutableDictionary
[myDict removeAllObjects];


This should hep you figure out which key (or keys) are pointing to the misbehaving arrays.

 for ( NSString *key in [myDict allKeys]) {
      NSLog(@" key - %@", key);
      [myDict removeObjectForKey: key];
 }
 NSLog(@" the problem is not with the dictionary...");


You don't need to empty an NSMutableDictionary before releasing. Any objects retained by the dictionary will sent a release message when the dictionary is dealloced.

It sounds as though your arrays are being retained elsewhere, causing the leak. Are you sure you've implemented dealloc methods correctly for all your objects (view controllers, model objects, etc)?

0

精彩评论

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

关注公众号