My app is working perfectly on the simulator but when I test the app on the device I eventually get a random EXC_BAD_ACCESS and the app crashes.

After a few days of testing I think I have found the code that is causing this error. At some point I need to add some subviews to a controller's main view, then the user interacts with the app and those subviews are removed from their superview and new subviews are added. If I don't ever remove the subviews the app does not crash but if I remove them, the app eventually gets an EXC_BAD_ACCESS and crashes.

It seems like the subviews removed get a release msg when they already are completely released or something like that... This the first app where I'm using ARC so I'm probably missing something...

Add an exception breakpoint for all exceptions and see if you can lock on to the line of code that's causing the crash.
–
StavashMay 21 '13 at 8:01

I tried that with no success. The app doesn't always crash in the same line. Only if the subviews are removed the app crashes, if I don't remove them the app does not crash. It must be some memory management issue.
–
pablomaticoMay 21 '13 at 8:15

Are your tags!=0 ? Because the view by default is 0 so you may remove other views that you want to remove.
–
danypataMay 21 '13 at 8:20

Yes. I've edited the question and added the tag constants. Sorry about that! This is working for a while and then crashes. I mean, the views are removed and added a few times and it seems like everything is OK but after a while the app gets the EXC_BAD_ACCESS.
–
pablomaticoMay 21 '13 at 8:25

you need to show us the stack trace. can't really help without it. As Stavash said, add an exception breakpoint and post the stack trace at the time of crash. also try enabling Zombies from Edit Scheme. this will give you a clue about which object is being over-released.
–
Mar0uxMay 21 '13 at 8:37

5 Answers
5

You're right, zombies will work only in simulator - though I suspect
the crash will reproduce in simulator as well. Very few cases where
app will crash on device, but not on simulator. Maybe give it another
try ensuring you use simulator of the same iOS version. I couldn't
find any info on _UIImageViewPretiledImageWrapper, but that will be
the key to solving this.

This answer suggests _UIImageViewPretiledImageWrapper is related to -[UIImage resizableImageWithCapInsets:].

Comments from OP:

At some point I was creating a UIImageView based on a resizable
UIImage but I was setting the image before setting the final size of
the UIImageView. This seems to somehow end up corrupting memory due to
the resizing of the UIImage

At some point I was creating a UIImageView based on a resizable UIImage but I was setting the image before setting the final size of the UIImageView. This seems to somehow end up corrupting memory due to the resizing of the UIImage.
–
pablomaticoMay 22 '13 at 6:09

I also had a crash similar to this and the fix was actually not in the code, but in the asset itself.

As Apple Documentation states the resizable area of the asset needs to be 1px by 1px, so ensure that it is.

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image. This technique is often used to create variable-width buttons, which retain the same rounded corners but whose center region grows or shrinks as needed. For best performance, use a tiled area that is a 1x1 pixel area in size.