Skip to content

Instantly share code, notes, and snippets.

@xareelee
Created August 4, 2015 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xareelee/162e436555ef82b83c9b to your computer and use it in GitHub Desktop.
Save xareelee/162e436555ef82b83c9b to your computer and use it in GitHub Desktop.
Track deallocated object for specific classes using XAspect
// Just copy the fllowing code into a .m file, and include the XAspect to you project.
// Then you'll see console log messages when objects of specific classes were deallocated.
// The block `AspectPatch(-, void, dealloc)` will be invoked just before `-dealloc` aka 'before advise'.
// Using XAspect
#import <XAspect/XAspect.h>
// Headers for classes you want to track
#import <CMDPUserLogin/UAMLoginViewController.h>
#import <CMDPUserLogin/UAMLoginTableViewController.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "UAMPhoneRegisterationTableViewController.h"
#define AtAspect DeallocTracker
#define AtAspectOfClass NSObject
@classPatchField(NSObject)
AspectPatch(-, void, dealloc)
{
static NSArray *klasses;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Add classes into this array for which you want to track when they were deallocated.
klasses = @[[UAMLoginViewController class],
[UAMLoginTableViewController class],
[RACCommand class],
[UAMPhoneRegisterationTableViewController class],
// More classes...
];
});
Class klass = [self class];
if ([klasses containsObject:klass]) {
NSLog(@"--%@ <%p> dealloc", NSStringFromClass(klass), self);
}
// Invoke the original -dealloc
XAMessageForwardDirectly(dealloc);
}
@end
#undef AtAspectOfClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment