views:

156

answers:

4

This line of code: [mymutabledict setObject:myclassobj forKey:myclassobj.myidstring];

Is giving this error:

*** -[NSCFString hash]: message sent to deallocated instance 0x3c14610

I put in a breakpoint on that line and I've checked and all 3 objects (mymutabledict, myclassobj, and myclassobj.myidstring) are correctly allocated and visible and correctly there in the debugger. What else could be causing this error? Could it be something to do with the object or keys I used to put previous items into the dictionary?

I've even checked and the key used does not match any of the previously entered keys.

For a little extra info: I'm loading objects from an SQL database to a mutable dictionary in memory. This code has all worked fine on several sets of sample data for weeks and just today this came up with one specific copy of the DB. Thanks!

+1  A: 

Try and post all the relevant code. That will help us understand the bigger picture.

Mihir Mathuria
+1  A: 

You seem to be calling release too many times on something. Add the following code just above that line:

for (NSString *key in mymutabledict) {
    NSLog(@"Key %@ maps to %@.", key, [mymutabledict objectForKey:key]);
}
NSLog(@"About to add %@", myclassobj);
NSLog(@"   with key %@", myclassobj.myname);

... and look for the crashing object.

Keys are copied, not retained, when objects are added to an NSDictionary / NSMutableDicitonary object. If you're pulling the keys from the dictionary and releasing them (e.g., calling [key release] in the above code or on string objects acquired via [mymutabledict allKeys]), that might do it.

John Franklin
I think this is leading towards the answer. Using your code I've noticed that the issue occurs when attempting to add an item with a duplicate ID to the dictionary. Now I have to find a way to check if the dictionary has something at that key before attempting to store the new object there...
mjdth
This was it. What I ended up doing was creating a mutable array and storing all of the values that I used for keys to make sure I wasn't assigning multiple objects to the same key. It seems to have fixed the problem, but I'm not sure why.
mjdth
+1  A: 

Run your program with NSZombieEnabled. NSZombieEnabled changes the type of the object(in your case an NSMutableDictionary) to an NSZombie object when the retain count hits 0. Whenever a message is sent to this zombie object, the program immediately crashes, allowing you to see exactly where the problem is occuring.

Mike
Follow the link to learn how to find out which method is called when overreleasing. This will help you target your efforts.
Alex Reynolds
A: 

How do you define the "myidstring" property ? As it is a NSString, you have declared it as "@property(retain)" in order to keep its reference valid.

Laurent Etiemble