Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2010, 12:45 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 11
matsoftware is on a distinguished road
Exclamation iOS4 issue with dictionary

Hi everybody. I've update the SDK from 3.1.3 to 4.0. I opened a project (and I didn't make any change on it), then I ran it on simulator and it crashed with this error on console:

Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5f8b2d0'
...
28  CoreFoundation                      0x0248eff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
	29  CoreFoundation                      0x023ef807 __CFRunLoopDoSource1 + 215
	30  CoreFoundation                      0x023eca93 __CFRunLoopRun + 979
...
This error occurs when I use the dictionary: objectForKey method. XCode gives me no warning about any obsolete method or stuff like that.

Simply, It doesn't work with 4.x firmware. Any idea about it? Thank you :-)
matsoftware is offline   Reply With Quote
Old 09-29-2010, 01:12 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

The dictionary is probably being deallocated prematurely and its pointer left dangling. You can tell this because the message is actually being received by a string ([NSCFString objectForKey:]) instead of an NSDictionary. This usually happens because you've failed to retain the dictionary when you should have, or you've released the dictionary before you were actually done with it. In the meantime, the OS has reclaimed the deallocated memory and put something else in its place, and so your NSDictionary pointer now points to something else -- in this case, a string. To fix this, you need to go back through your code and make sure you're retaining and releasing the dictionary at the proper times.

The fact that this bug seemed to appear out of nowhere when changing something about the platform is pretty typical. Dangling pointers and other memory management bugs result in undefined behavior, which means that sometimes things can appear to be working normally even when there's a logic error in the code. In other words, the bug was probably always there, but it was just a coincidence that it didn't cause a crash under 3.x.
ChrisL is offline   Reply With Quote
Old 09-30-2010, 02:55 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 11
matsoftware is on a distinguished road
Default

Thank you for your fast reply :-)

I've found in my code that this error occurs when I retrieve data from a .plist file. (I have a list of element that a user can manage by adding, moving, editing data - like a list of book - and this list is saved in the Documents folder of the App). The code I use to retrieve data is the following:


Code:
+ (NSString *)pathForDocumentWithName:(NSString *)documentName
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, 
                                                         YES);
    NSString *path = [paths objectAtIndex:0];
    
    return [path stringByAppendingPathComponent:documentName];
}

- (NSMutableArray *)displayedObjects
{
    if (_displayedObjects == nil)
    {
        NSString *path = [[self class] pathForDocumentWithName:@"Books.plist"];
        NSArray *booksDicts = [NSMutableArray arrayWithContentsOfFile:[path stringByStandardizingPath]];
        
        if (booksDicts == nil)
        {
            NSLog(@"Unable to read plist file at path: %@", path);
            path = [[NSBundle mainBundle] pathForResource:@"Books"
                                                   ofType:@"plist"];
            booksDicts = [NSMutableArray arrayWithContentsOfFile:path];
        }
        
        _displayedObjects = [[NSMutableArray alloc]
                             initWithCapacity:[booksDicts count]];
          
        for (NSDictionary *currDict in booksDicts)
        {
            Book *books = [[Book alloc] initWithDictionary:currDict];
            [_displayedObjects addObject:books];
        }
    }
    
    return _displayedObjects;
}
where "Book" is the class that manage the list. If there is no file (like the first use) I don't have problem with the calling of the class "Book" and the dictionary: objectForKey . The error occurs in the bold row, and I don't know how figure it out.
matsoftware is offline   Reply With Quote
Reply

Bookmarks

Tags
dictionary, ios4

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
16 members and 370 guests
AragornSG, ChrisYates, coolman, davejas69, givensur, hussain1982, iAppDeveloper, jbro, Kryckter, locombiano89, Mah6447, Meoz, stanny, stevenkik, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:46 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0