Skip to content

Instantly share code, notes, and snippets.

@bdash
Created January 5, 2020 17:44
Show Gist options
  • Save bdash/2105ae0c7039c5d77aee82d8df385738 to your computer and use it in GitHub Desktop.
Save bdash/2105ae0c7039c5d77aee82d8df385738 to your computer and use it in GitHub Desktop.
Demonstrate a case of a spurious warning about an instance being immediately deallocated
#import <Foundation/Foundation.h>
@interface RetainCycle : NSObject
@end
@implementation RetainCycle {
id _object;
}
- (instancetype)init {
if (!(self = [super init])) {
return nil;
}
_object = self;
return self;
}
- (void)dealloc {
NSLog(@"-[%@ dealloc]", self);
}
- (void)breakCycle {
_object = nil;
}
@end
int main(int argc, char** argv) {
__weak RetainCycle *weak = [[RetainCycle alloc] init];
NSLog(@"weak: %@", weak);
[weak breakCycle];
NSLog(@"weak: %@", weak);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment