If you could use C++, check Boost's Scope Exit library.
If you don't mind having to type 2 extra words in the beginning and the end of the function, you could use the @finally block.
#define SCOPE {id _defered_actions__=[[NSMutableArray alloc]init];@try{
#define END_SCOPE }@finally{for(void(^action)()in[_defered_actions__ reverseObjectEnumerator])action();[_defered_actions__ release];}}
#define DEFER_COPY(_code__) {id _blk__=[^{_code__;}copy];[_defered_actions__ addObject:_blk__];[_blk__ release];}
#define DEFER(_code__) ([_defered_actions__ addObject:(^{_code__;})])
Example use:
@interface XXObject : NSObject {
}
-(int)factorial:(int)x;
@end
@implementation XXObject
-(int)factorial:(int)x { SCOPE
printf("begin foo:%d\n", x);
DEFER( printf("end foo:%d\n", x) );
if (x > 0)
return x * [self factorial:x-1];
else if (x == 0)
return 1;
else {
@throw [NSException exceptionWithName:@"NegativeFactorialException"
reason:@"Cannot call factorial on negative numbers"
userInfo:nil];
return 0;
}
END_SCOPE }
-(void)dealloc {
printf("%p has been released.\n", self);
[super dealloc];
}
@end
void do_stuff() { SCOPE
__block XXObject* x = [[XXObject alloc] init];
DEFER({
printf("releasing %p.\n", x);
[x release];
});
int i;
for (i = 2; i >= -1; -- i) {
// use DEFER_COPY to retain the local variable 'i' and 'fact'
int fact = [x factorial:i];
DEFER_COPY( printf("%d! == %d\n", i, fact) );
}
END_SCOPE }
int main () {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@try {
do_stuff();
} @catch(NSException* e) {
// note that the @finally statements might not be called in 64-bit if we
// left the exception uncaught.
NSLog(@"%@", e);
}
[pool drain];
return 0;
}
Which should print:
begin foo:2
begin foo:1
begin foo:0
end foo:0
end foo:1
end foo:2
begin foo:1
begin foo:0
end foo:0
end foo:1
begin foo:0
end foo:0
begin foo:-1
end foo:-1
0! == 1
1! == 1
2! == 2
releasing 0x100116500.
0x100116500 has been released.
2011-02-05 23:06:21.192 a.out[51141:903] Cannot call factorial on negative numbers