1

I've a tableviewcontroller which stores dictionary objects in an Array, when a row gets selected I push a new view controller on the navigation stack and I pass a dictionary object from the array in it's initializer, the dictionary object will be retained in a property of the new view controller. When I return back to my tableviewcontroller I release the dictionary object in the dealloc of the view controller. But when I scroll down/back in my tableviewcontroller, the dictionary that was passed to the view controller is deallocated in the tableviewcontroller's Array and the program crashes with the message:

BreakdanceSource[1351:607] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key title.'

When I remove the [dictionary release] from the dealloc function of the view controller, the program works and doesn't crash! It's weird because I thought the array also keeps ownership of it's objects so the dictionary shouldn't have been deallocated.

Why can't I release the dictionary object in my view controller? (I've removed the [dictionary release] now in the dealloc and the program works but won't I have a memory leak? )

I've tried this so far!:

imageshack.us/photo/my-images/813/screenshot20110926at610.png I've used the zombies tool and this is my result! What I don't get is that before the dealloc of my view controller the retain count is 1 which should've been 2. Because the array in the tableviewcontroller should own the dictionary and I've a property in my view controller which is @property (retain) NSDictionary* media. I don't know where my dictionary gets released, because now it only gets released in the view controller's dealloc

I've also tried to change the property @property (retain) to @property NSDictionary* media then I use media = [infoDictionary retain] and it doesn't crash.. did my property (retain) not retain it?

qkp
  • 161
  • 1
  • 1
  • 12
  • What function are you using to get the entry from the array? If it's removeObjectAtIndes vs objectAtIndex, eg, you'll obviously be removing the dictionary from the array. But more likely you're just doing a double release somehow. – Hot Licks Sep 25 '11 at 20:46
  • I'm using [media objectAtIndex:indexPath.row]; so the array shouldn't have released the thing! – qkp Sep 26 '11 at 16:36

1 Answers1

1

Yes NSMutableArray increases the retain count of an object that is added to it and correspondingly decreases the retain count when the NSMutableArray is released. That does not stop something else from decrementing the retain count to the point of an objects release. So, there must be another release/autorelease of the object.

Use Instruments Allocation tool, set the retain count option, find the object and you will see where the extra release is happening. See this SO answer for info on using Instruments in this manner.

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228
  • http://imageshack.us/photo/my-images/813/screenshot20110926at610.png/ I've used the zombies tool and this is my result! What I don't get is that before the dealloc of my view controller the retain count is 1 which should've been 2. Because the array in the tableviewcontroller should own the dictionary and I've a property in my view controller which is @property (retain) NSDictionary* media. I don't know where my dictionary gets released, because now it only gets released in the view controller's dealloc. – qkp Sep 26 '11 at 16:29