Tom Insam

NSAssert in blocks

From NSNotificationCenter with blocks considered harmful:

what is wrong here? I’ll give you a hint: if you test in Release mode, it works fine. It only fails in Debug mode.

Give up?

Here’s the answer:

NSAssert(counter==oldCounterValue+1, @”Atomicity guarantee violated.”);

See, NSAssert is a macro. A macro that expands to this:

do {
      if (!(condition)) {
        [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd
        object:self file:[NSString stringWithUTF8String:__FILE__]
            lineNumber:__LINE__ description:(desc), ##__VA_ARGS__];
      } while(0)

See there? Big fat self. Ergo, retain cycle, ergo test failure