Created
October 18, 2014 04:50
-
-
Save uasi/1c46f840708f20142757 to your computer and use it in GitHub Desktop.
EXNOnDealloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EXNOnDealloc.h | |
// | |
// Copyright (c) 2014 uasi. All rights reserved. | |
// | |
#import <libextobjc/metamacros.h> | |
#define onDeallocOfObject(object) \ | |
autoreleasepool {} \ | |
__strong EXNFinalizer *metamacro_concat(exn_finalizer_, __LINE__) = [[EXNFinalizer alloc] init]; \ | |
exn_associateFinalizer(object, metamacro_concat(exn_finalizer_, __LINE__)); \ | |
metamacro_concat(exn_finalizer_, __LINE__).finalize = ^ | |
#define onDealloc onDeallocOfObject(self) | |
typedef void (^exn_finalizerBlock_t)(); | |
@interface EXNFinalizer : NSObject | |
@property (nonatomic, copy) exn_finalizerBlock_t finalize; | |
@end | |
void exn_associateFinalizer(__strong id self, __strong EXNFinalizer *finalizer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EXNOnDealloc.m | |
// | |
// Copyright (c) uasi. All rights reserved. | |
// | |
#import "EXNOnDealloc.h" | |
#import <objc/runtime.h> | |
void exn_associateFinalizer(__strong id self, __strong EXNFinalizer *finalizer) { | |
objc_setAssociatedObject(self, (__bridge void *)finalizer, finalizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
@implementation EXNFinalizer | |
- (void)dealloc { | |
if (self.finalize) self.finalize(); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment