The culprit is the line [invocation retainArguments]
. If you comment out that line, it works fine. (That line was never necessary anyway because the invocation is never stored or used asynchronously.)
Explanation:
Think about what -retainArguments
does. It calls retain
on all the arguments of object pointer type. And then when the invocation is deallocated, it calls release
on them.
But the arguments are stack (non-copied) blocks. retain
and release
have no effect on it (since it's not a heap object). So when it is retained, nothing happens, and then (from the crash) it appears that the invocation was autoreleased at some point (a perfectly normal thing to happen), so the final release and deallocation of the invocation happens asynchronously. When the invocation is deallocated, it tries to release its retained arguments, but by then it's trying to message a no-longer valid stack block, causing a crash.
P.S. the copying of the blocks in forwardInvocation:
was also unnecessary