#import // won't invoke -dealloc // clang -g -Wall -DRETAIN_CYCLE=1 -framework Foundation -o blockcycle blockcycle.m // will invoke -dealloc // clang -g -Wall -DRETAIN_CYCLE=0 -framework Foundation -o blockcycle blockcycle.m // Just a simple block pointer that asks for nothing and gives nothing. typedef void (^BlockHead)(void); // The leaky object. @interface Leakzor : NSObject { NSString *string; BlockHead blockhead; } // Print |string|. - (void) funk; @end // Leakzor @implementation Leakzor #if RETAIN_CYCLE - (id) init { if ((self = [super init])) { string = @"snork"; // |string| is the same as self->string, so |self| is retained. blockhead = Block_copy(^{ NSLog (@"string is %@", string); }); } return self; } // init #else - (id) init { if ((self = [super init])) { string = @"snork"; // blockSelf is __block scope, so won't be auto-retained __block Leakzor *blockSelf = self; blockhead = Block_copy(^{ NSLog (@"string is %@", blockSelf->string); }); } return self; } // init #endif - (void) dealloc { NSLog (@"dealloc"); [super dealloc]; } // dealloc - (void) funk { blockhead (); } // funk @end // Leakzor int main (void) { Leakzor *leaky = [[Leakzor alloc] init]; [leaky funk]; [leaky release]; return 0; } // main