views:

55

answers:

2

Part of my app downloads images from the Internet and stores them in a mutable array. This is so that they do not have to be downloaded every time the user wants to view them. When I exit the view that deals with these images, the mutable array gets released. If there is only one image stored in the array, everything runs smoothly. However, if there is more than one image, the app crashes with an EXC_BAD_ACCESS. Using NSZombies, I get the following error:

*** -[UIImage isKindOfClass:]: message sent to deallocated instance 0x5b8eae0

However, I do not use the isKindOfClass message in my app, so it's obviously being sent by some other system method. What is causing this message to be sent, how do I go about fixing this issue, and why is it only happening if there is more than one image in the array? Thanks.

A: 

Use the Zombies instrument in Instruments. (Run -> Run With Performance Tools -> Zombies)

Instruments can tell you exactly where your code messaged a deallocated object. More importantly, it can tell you all the retains and releases prior and, thus, exactly where and what was over-released.

See the Instruments documentation for more information.

bbum
I'll check it out. Just quickly, why would Zombie be greyed out in the drop down menu in Xcode?
churchill614
A: 

Sounds like you're over-releasing your image objects. Like all the Cocoa collection classes, an NSMutableArray instance retains objects added to it and releases them when it is itself released. If you create an object using a call that doesn't increase its retain count and then release it once you've added it to the array, you'll see this sort of error.

For example:

UIImage *image = [UIImage imageWithData:someData];
[myArray addObject:image];
[image release]; // <-- BAD!

Here you don't own the object image (because you didn't create it using alloc or copy), so you don't need to release it. The pointer in the array now points to an object with a retain count of zero, which will eventually be deallocated.

Using autorelease instead of release in the above example is equally wrong, for the same reason.

Simon Whitaker
Thanks. That helped a lot! Much appreciated,
churchill614
Great news! Glad it helped.
Simon Whitaker