Strongify is an open-source GitHub project that offers a macro library to prevent block retain cycles. It provides two macros, @weakify and @strongify, designed to handle weak and strong references within blocks.
Retain Cycles
In Objective-C, when referencing self within a block, and self retains the block while the block retains self, it leads to a retain cycle, causing memory leaks. For example:
@interface TestViewController : UIViewController
@property (nonatomic, strong) void (^block)(void);
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^{
[self doSomething];
};
}
- (void)doSomething {
// ...
}
@end
In the code above, a retain cycle is formed as self retains the block, and the block retains self. When self is deallocated, the block still holds a reference to self, preventing it from being released and resulting in a memory leak.
Strongify and Weakify
@weakify and @strongify macros are used to create weak references and then strong references within blocks, respectively.
@weakify(self): Weakly referencesself.@strongify(self): Strongly references the weak reference toself.
Using strongify and weakify effectively prevents block retain cycles. For example:
@import strongify;
@interface TestViewController : UIViewController
@property (nonatomic, strong) void (^block)(void);
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^{
@weakify(self);
[self doSomething];
@strongify(self);
// ...
};
}
- (void)doSomething {
// ...
}
@end
In the code above, @weakify(self) creates a weak reference to self, and @strongify(self) creates a strong reference to the weak reference. This ensures that when the block execution is complete, self can be released correctly, preventing a retain cycle.
Usage Notes
strongifyandweakifyshould only be used inside blocks.- They cannot be used with class methods.
- They cannot be used with static variables.
Conclusion
Strongify is a simple and easy-to-use macro library that effectively prevents block retain cycles. It helps developers avoid memory leaks, thereby improving code quality.