priore

Tips: a solid dealloc for all classes

Jun 23rd, 2014
287
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <objc/runtime.h>
  2.  
  3. - (void)dealloc
  4. {
  5.     [CATransaction setCompletionBlock:nil];
  6.     [NSObject cancelPreviousPerformRequestsWithTarget:self];
  7.     [[NSNotificationCenter defaultCenter] removeObserver:self];
  8.    
  9.     if ([self respondsToSelector:@selector(setDelegate:)])
  10.             [self performSelector:@selector(setDelegate:) withObject:nil];
  11.    
  12.     // invalidate timers
  13.     unsigned int count = 0;
  14.     objc_property_t *properties = class_copyPropertyList([self class], &count);
  15.     for (int i = 0; i < count; ++i) {
  16.         NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
  17.         id prop = [self valueForKey:name];
  18.         if ([prop respondsToSelector:@selector(invalidate)])
  19.             [prop invalidate], prop = nil;
  20.     }
  21.  
  22.     [super dealloc]; // not needed in ARC
  23. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×