Problems accessing the content of NSMutableDictionary

Discussion in 'Mac Programming' started by Nicsoft, Dec 21, 2009.

  1. Nicsoft macrumors member

    Oct 17, 2009

    When running this code, the application crashes.

    NSLog(@"rowDictionary %i count %i ", rowDictionary, [rowDictionary count]);
    NSArray *theKeys = [rowDictionary allKeys];	
    for(int i = 0;i<[theKeys count];i++){
    	NSLog(@"KEY: %@", (NSString*)([theKeys objectAtIndex:i]));		
    It crashes on the second row. The problem happens even if the there are several entries in the rowDictionary.

    When wrapping the code inside a try block, the error is not caught in "@catch (NSException *exception)".

    rowDictionary is an instance variabel of type NSMutableDictionary.

    I've tried a lot of different ways retreiving the objects without success. How can I find out if the entries in the dictionary wasn't added properly, all my attempt trying that has failed for me?

    Thank´s in advance!

    P.S. Tried to find info about what the "Post a Poll" option is, didn't find anything. What's that? D.S.
  2. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    One suggestion would be to use fast enumeration if you can support 10.5+ only.

    for (NSString *key in rowDictionary) {
        	NSLog(@"KEY: %@", key);		
  3. kpua macrumors 6502

    Jul 25, 2006
    Posting the crash report would provide a little more insight, but my first intuition is that you've accidentally over-released a key in the dictionary, so attempting to print the object's description, which accesses the free'd pointer, is causing the crash.

    EDIT: Actually, never mind. The likelihood of that is very slim, since NSDictionary copies its keys. Again, the crash report would be useful.
  4. Nicsoft thread starter macrumors member

    Oct 17, 2009
    Sorry, I'm quite a beginner: Forgot to mention, this is for iPhone, but I didn't consider it to be an iPhone problem, more a problem of how to use the dictionary, hence I posted here. Incorrect of me?

    The crash report is attached, and copied into the post below.

    Tried below, but it crashe on the second row (NSLog(@"KEY: %@", key); )

    Process: BlockPop [25794]
    Path: /Users/Niklas/Library/Application Support/iPhone Simulator/User/Applications/42F37CD7-C308-4931-8FB1-781B0F54FC7F/
    Identifier: BlockPop
    Version: ??? (???)
    Code Type: X86 (Native)
    Parent Process: launchd [74]

    Date/Time: 2009-12-21 21:57:25.963 +0100
    OS Version: Mac OS X 10.6.2 (10C540)
    Report Version: 6

    Exception Type: EXC_BAD_ACCESS (SIGBUS)
    Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
    Crashed Thread: 0 Dispatch queue:

    Application Specific Information:
    objc_msgSend() selector name: retain
    iPhone Simulator 3.1 (139.1), iPhone OS 3.1.2 (7D11)

    Thread 0 Crashed: Dispatch queue:
    0 libobjc.A.dylib 0x93fe2edb objc_msgSend + 27
    1 CoreFoundation 0x01b87495 CFArrayCreate + 197
    2 CoreFoundation 0x01bca6c3 -[__NSPlaceholderArray initWithObjects:count:] + 515
    3 CoreFoundation 0x01be734a +[NSArray arrayWithObjects:count:] + 74
    4 CoreFoundation 0x01bff05e -[NSDictionary allKeys] + 142
    5 BlockPop 0x00003e46 -[BoardViewController removDirection:] + 506 (BoardViewController.m:65)
    6 UIKit 0x002a9459 -[UIApplication sendAction:to:from:forEvent:] + 119
    7 UIKit 0x0030cba2 -[UIControl sendAction:to:forEvent:] + 67
    8 UIKit 0x0030edc3 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 478
    9 UIKit 0x0030db0f -[UIControl touchesEnded:withEvent:] + 442
    10 UIKit 0x002c2e33 -[UIWindow _sendTouchesForEvent:] + 507
    11 UIKit 0x002ac81c -[UIApplication sendEvent:] + 400
    12 UIKit 0x002b30b5 _UIApplicationHandleEvent + 5048
    13 GraphicsServices 0x023b8ed1 PurpleEventCallback + 1533
    14 CoreFoundation 0x01bcbb80 CFRunLoopRunSpecific + 3888
    15 CoreFoundation 0x01bcac48 CFRunLoopRunInMode + 88
    16 GraphicsServices 0x023b778d GSEventRunModal + 217
    17 GraphicsServices 0x023b7852 GSEventRun + 115
    18 UIKit 0x002b4003 UIApplicationMain + 1157
    19 BlockPop 0x00001e3c main + 102 (main.m:14)
    20 BlockPop 0x00001daa start + 54

    Thread 1:
    0 libSystem.B.dylib 0x98e348da mach_msg_trap + 10
    1 libSystem.B.dylib 0x98e35047 mach_msg + 68
    2 CoreFoundation 0x01bcb382 CFRunLoopRunSpecific + 1842
    3 CoreFoundation 0x01bcac48 CFRunLoopRunInMode + 88
    4 WebCore 0x025cd803 RunWebThread(void*) + 467
    5 libSystem.B.dylib 0x98e61fbd _pthread_start + 345
    6 libSystem.B.dylib 0x98e61e42 thread_start + 34

    Thread 0 crashed with X86 Thread State (32-bit):
    eax: 0x0392d790 ebx: 0x01b87911 ecx: 0x92873be0 edx: 0x0392d84a
    edi: 0x00000000 esi: 0x0392d790 ebp: 0xbfffdcf8 esp: 0xbfffdcd4
    ss: 0x0000001f efl: 0x00010206 eip: 0x93fe2edb cs: 0x00000017
    ds: 0x0000001f es: 0x0000001f fs: 0x00000000 gs: 0x00000037
    cr2: 0x00000000

    Binary Images:
    0x1000 - 0xcff7 +BlockPop ??? (???) <A8C75002-AFF3-13C6-E24E-D421195579E3> /Users/Niklas/Library/Application Support/iPhone Simulator/User/Applications/42F37CD7-C308-4931-8FB1-781B0F54FC7F/
    0x18000 - 0x1aff7 +PBGDBIntrospectionSupport.A.dylib ??? (???) <7C5F8E70-6B87-C6B1-F1DD-79DD6300B04A> /Developer/Applications/
    0x1f000 - 0x1aaff3 +Foundation ??? (???) <202A4D63-19E4-DDDB-F670-0524F3B86144> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/Foundation.framework/Foundation
    0x29f000 - 0x165cff7 +UIKit ??? (???) <B69694CB-1309-C687-4784-4BFDAD0C5C21> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/UIKit.framework/UIKit
    0x1914000 - 0x1b1cfe7 1.359.16 (???) <598212DE-C4BA-1E81-6AA2-82B73438C190> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics
    0x1b83000 - 0x1c89ffb +CoreFoundation ??? (???) <B7589A53-6DB2-3582-1060-A36D65D525BF> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
    0x1d6d000 - 0x1ee3fe7 +libicucore.A.dylib ??? (???) <CFE882B3-BBF0-FD07-D0B4-700DD7FD0402> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/usr/lib/libicucore.A.dylib
    0x1f3d000 - 0x203cfe7 +libxml2.2.dylib ??? (???) <B67B2086-934F-9413-F4F7-0E758E255760> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/usr/lib/libxml2.2.dylib
    0x2063000 - 0x20fdfe7 +CFNetwork ??? (???) <F886292F-F33A-E53D-FBD0-3DA5422A7E0E> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CFNetwork.framework/CFNetwork
    0x216d000 - 0x21a1ff7 +SystemConfiguration ??? (???) <D2582BBD-99A8-DB80-A9DA-F7CF35018B4A> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration
    0x21c3000 - 0x2217ff7 +Security ??? (???) <F067BF34-69D6-C395-CD54-2C565E9CB8C5> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/Security.framework/Security
    0x223d000 - 0x22f3ff7 +libsqlite3.dylib ??? (???) <EBB2A3B8-0AC7-3AF3-8FF6-5F2E60813D7C> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/usr/lib/libsqlite3.dylib
    0x2302000 - 0x238dfff 1.5.1 (???) <A17F9F5E-A7E8-016A-467E-67349F4D3D03> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
    0x23b3000 - 0x23bcff7 +GraphicsServices ??? (???) <038A27BA-129E-1B32-2F14-A04B8187E899> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
    0x23cc000 - 0x24f4fe3 +JavaScriptCore ??? (???) <405E1C67-B593-C6AD-08DF-50EEDA2B0A11> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore
    0x2548000 - 0x2c78fe7 +WebCore ??? (???) <C218CFD6-360B-7400-2A16-FFFDB1425996> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/WebCore.framework/WebCore
    0x3182000 - 0x320cff7 +WebKit ??? (???) <D600BBA3-6D73-FC45-0B99-894B9023A56C> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/WebKit.framework/WebKit
    0x3286000 - 0x3340fe7 +ImageIO ??? (???) <65150BDC-A3A8-D3CE-0578-22A4C1E8C191> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/ImageIO.framework/ImageIO
    0x336a000 - 0x3388ff7 +AddressBook ??? (???) <AEA5281E-5C24-8D7D-9900-8DE7C3D84110> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/AddressBook.framework/AddressBook
    0x33a9000 - 0x34dffe7 +AudioToolbox ??? (???) <5A7408F7-4035-9AD1-6020-44CD24886F5E> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox
    0x353e000 - 0x35dbff7 +QuartzCore ??? (???) <6588279F-9A85-E18D-5B5B-2E0D2D7368E9> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/QuartzCore.framework/QuartzCore
    0x3624000 - 0x3630ff7 +SpringBoardServices ??? (???) <790CBACF-CF9E-0858-6CBD-03480A0290BF> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices
    0x363b000 - 0x3667fff +AppSupport ??? (???) <27EE9C24-ACB7-FA9C-2811-A1E8C1BF06AC> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport
    0x3688000 - 0x36c5ff7 +CoreText ??? (???) <B74A41B0-6DAF-EE68-711E-ED10B98736D9> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/CoreText.framework/CoreText
    0x36f3000 - 0x3701ff7 +MobileCoreServices ??? (???) <3549C90D-C706-99EF-4FCD-D0B5CCC52D83> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices
    0x370c000 - 0x3717ff7 +CoreVideo ??? (???) <EE83A90A-77E9-5DAD-BDBA-9817A5B2631E> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/PrivateFrameworks/CoreVideo.framework/CoreVideo
    0x3723000 - 0x3739fe7 +OpenGLES ??? (???) <03F14589-A199-3785-38B9-64EA677BE805> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/OpenGLES.framework/OpenGLES
    0x3743000 - 0x37b2fff +CoreAudio ??? (???) <FAE4E585-9B6B-75D0-C26C-432F23828A52> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CoreAudio.framework/CoreAudio
    0x3841000 - 0x3883fe7 +libCGFreetype.A.dylib ??? (???) <E26AC38A-1C54-C842-A484-68E07F999112> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libCGFreetype.A.dylib
    0x38b3000 - 0x38d9fe7 +libRIP.A.dylib ??? (???) <8C82177E-AE4A-E9F2-240C-E1A2DEA808E4> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libRIP.A.dylib
    0x8fe00000 - 0x8fe4162b dyld 132.1 (???) <211AF0DD-42D9-79C8-BB6A-1F4BEEF4B4AB> /usr/lib/dyld
    0x90521000 - 0x9052ffe7 libz.1.dylib ??? (???) <7B7A02AB-DA99-6180-880E-D28E4F9AA8EB> /usr/lib/libz.1.dylib
    0x91b47000 - 0x91b4afe7 libmathCommon.A.dylib ??? (???) <1622A54F-1A98-2CBE-B6A4-2122981A500E> /usr/lib/system/libmathCommon.A.dylib
    0x92bf1000 - 0x92c5bfe7 libstdc++.6.dylib ??? (???) <411D87F4-B7E1-44EB-F201-F8B4F9227213> /usr/lib/libstdc++.6.dylib
    0x932ae000 - 0x932f4ff7 libauto.dylib ??? (???) <85670A64-3B67-8162-D441-D8E0BE15CA94> /usr/lib/libauto.dylib
    0x93fdd000 - 0x9408afe7 libobjc.A.dylib ??? (???) <DF8E4CFA-3719-3415-0BF1-E8C5E561C3B1> /usr/lib/libobjc.A.dylib
    0x98e1f000 - 0x98e33fe7 libbsm.0.dylib ??? (???) <14CB053A-7C47-96DA-E415-0906BA1B78C9> /usr/lib/libbsm.0.dylib
    0x98e34000 - 0x98fd8feb libSystem.B.dylib ??? (???) <D45B91B2-2B4C-AAC0-8096-1FC48B7E9672> /usr/lib/libSystem.B.dylib
    0xffff0000 - 0xffff1fff libSystem.B.dylib ??? (???) <D45B91B2-2B4C-AAC0-8096-1FC48B7E9672> /usr/lib/libSystem.B.dylib

    Attached Files:

  5. chown33 macrumors 604

    Aug 9, 2009
    NSLog(@"rowDictionary %i count %i ", rowDictionary, [rowDictionary count]);
    The first format-specifier shouldn't be %i, unless what you want is the decimal number representation of rowDictionary's address. If that's not what you want, then %i is wrong but probably not fatal.

    What happens with this code:
    NSLog(@"rowDictionary %@", rowDictionary);
    If it crashes, then odds are good that rowDictionary is damaged or is invalid in some way. In that case, you need to show your code for how rowDictionary was made and/or populated.
  6. Nicsoft thread starter macrumors member

    Oct 17, 2009
    Actually, I wanted to see that it has an adress.

    It crashes...

    rowDictionary is an instance variable, and a property. It's synthesized alright.

    1. In touchesEnded I do this:

    rowDictionary = [[NSMutableDictionary alloc] init];
    No difference in behaviour if I retain it.

    2. Then I call upon a method (hideBrics) that populate rowDictionary.

    3. Inside hideBrics I do like this when populating rowDictionary:

    popRow = [[PopRow alloc]init:xTouched:(nextY)];
    rowKey = [NSString stringWithFormat: @"%i", (nextY)];
    [rowDictionary setObject:popRow forKey:rowKey];
    [popRow release];
    [rowKey release];
    This is performed several times.

    4. The last step is the execution of the code we are discussing now. What happens is that after hideBrics, a new view is presented. The user touches a button that is connected to an IBAction in which the code I am having problem with is located.

    Note: One odd thing, before I called the hideBrics from a different method, in which I also call other methods that are using rowDictionary (that is from the IBAction mentioned above). It was when I moved it that it started to crash, and I have to move it... I've been thinking about scooping, but all variables are instance variables defined in the .h file (rowKey, popRow as well). For debugging reason I am trying to access rowDictionary's population from the IBAction... Thanks for looking at it!
  7. kainjow Moderator emeritus


    Jun 15, 2000
    You are over-releasing rowKey. Remove the [rowKey release] line.
  8. gnasher729 macrumors P6


    Nov 25, 2005
  9. Nicsoft thread starter macrumors member

    Oct 17, 2009
    Well of eyes aren't that trained yet beginner as I am... Thanks indeed. The problem was that I over-released the key.

    Well, it is my code, but posting all code would be too much, this is about the last things I am trying to get together for an entire game... But it's working so I guess I did post enough code. The parantheses was just legacy, no reason now.

    But I still can't understand why there is a difference in behaviour depending on where I access the method that populates rowDictionary... It shouldn't work at anytime if the key is over-release, should it? If anyone may have any idea, please fell free to share, otherwise I consider my problem to be solved right now.

    Thanks indeed for the time!
  10. kainjow Moderator emeritus


    Jun 15, 2000
    The string is created initially as an autoreleased string, which means the release method won't be called until the current autorelease pool itself is deallocated. Unless you have your own autorelease pool setup, you're using the pool created by the run loop. The run loop empties its own pool at the end of each run loop iteration. So, accessing the dictionary won't crash until the next run loop iteration because the retain count is still 1 at the point of creation in your code. But by the next run loop iteration, the autorelease pool will have emptied itself, thus calling release on your object, causing it to be deallocated, and after this point the pointer in the dictionary becomes invalid, thus causing a crash anytime the key is accessed.

    One important thing to know is if you had used an NSMutableString as the key, things would have worked slightly differently. You would have most likely gotten the crash in the next run loop iteration (virtually immediately). Dictionaries make a copy of the keys, but a copy of a immutable object simply retains it, so the dictionary in the case of a mutable key would work fine.
  11. Nicsoft thread starter macrumors member

    Oct 17, 2009
    That's make sense. It also explains the inconsistent behaviour I expreienced while having the problem, I guess. Thanks for the lesson, now I am a lot wiser.

Share This Page