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 {
    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS
    if (!(condition)) {
        [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd
        object:self file:[NSString stringWithUTF8String:__FILE__]
            lineNumber:__LINE__ description:(desc), ##__VA_ARGS__];
    }               
        __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS
} while(0)

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

Author: Tom Insam

programming / london / san francisco / bacon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s