objc_retain EXC_BAD_ACCESS

The Lazy Coder picture The Lazy Coder · Sep 14, 2012 · Viewed 7.8k times · Source

I have been having a bit of a bug while testing on iOS 6 with my current iOS 5 app.

We have experienced a lock up on a method return for an innocuous method that internally used blocks, but not as properties. The issue is that calling the method works, so does every line of code within the method (including the block utilizing code)

I tried using [block copy] before calling the block, but there was absolutely no change.

Answer

The Lazy Coder picture The Lazy Coder · Sep 14, 2012

turns out the function definition of my code was declared in an internal interface and did not have a return type.

Here are some graphics to illustrate this issue.

The Initial Error

The Initial Error

The Stack Track

The Stack Trace

The Method in Question (isolated from self to determine the issue exact location)

The Method in Question

The Function Implementation (this is what is called, and returned)

The Implementation Line

The Definition in the Private Interface

The Definition in the Private Interface

I decided to look at the function call, and noticed it returning (id) rather than void

Function returning (id)

And Finally the only code change that alleviated this bug.

Code Change

Explanation

This bug reared its ugly head when my client called me saying our app does not run on ios 6

I was forced to download iOS 6 and Xcode 4.5 for testing this out.

I did indeed crash every time the app was run.

After hunting down this bug on stack overflow among other sites linked to by Google, I tried the block issue that some others are experiencing. And did a copy wherever I could to try to alleviate the issue of retained object falling off the stack.

I was not using block properties so I just called copy on the blocks themselves.

This did not help.

Finally with another developer going over it with me. I was stepping back and looking at it from another angle, and decided to try to determine what the heck was being retained.

It turned out the result of the function was being retained. And the only way I figured that out was to look at the value that auto complete showed me as the return type.

I knew the return type to be void, however it was telling me that the return type was id and that is what sparked the investigation into the method definition.

I hope this helps others that have this issue as I spent about 2 hours hunting it down and it turned out to be a semantic issue between a result type that should never have existed.