Objective-C ARC Patterns by thebushidocollective/han
npx skills add https://github.com/thebushidocollective/han --skill 'Objective-C ARC Patterns'自动引用计数(ARC)是 Objective-C 的内存管理系统,它在编译时自动插入保留和释放调用。ARC 消除了大部分手动内存管理,同时提供了确定性的内存行为,并防止了常见的内存错误,如释放后使用和双重释放。
与垃圾回收不同,当引用计数达到零时,ARC 会立即释放内存,这使其适用于 iOS 等资源受限的环境。理解 ARC 的所有权规则、限定符和模式对于编写内存安全的 Objective-C 代码和避免循环引用至关重要。
本技能涵盖强引用和弱引用、所有权限定符、循环引用、Core Foundation 桥接以及基于 ARC 的内存管理最佳实践。
强引用维持对象的所有权并防止其释放,而弱引用观察对象但不阻止其释放。
// 强引用(默认)
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *friends;
@property (nonatomic, strong) UIImage *photo;
@end
@implementation Person
@end
// 弱引用
@interface ViewController : UIViewController
@property (nonatomic, weak) id<ViewControllerDelegate> delegate;
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@end
@implementation ViewController
@end
@protocol ViewControllerDelegate <NSObject>
- (void)viewControllerDidFinish:(ViewController *)controller;
@end
// 使用强引用和弱引用
void strongWeakExample(void) {
Person *person = [[Person alloc] init];
person.name = @"Alice"; // 对 NSString 的强引用
__weak Person *weakPerson = person; // 弱引用
NSLog(@"Weak person: %@", weakPerson.name);
person = nil; // Person 被释放
NSLog(@"After nil: %@", weakPerson); // weakPerson 现在为 nil
}
// 无主引用(unsafe_unretained)
@interface NodeOld : NSObject
@property (nonatomic, unsafe_unretained) NodeOld *parent;
@property (nonatomic, strong) NSArray<NodeOld *> *children;
@end
@implementation NodeOld
@end
// 弱引用 vs 无主引用
@interface CommentOld : NSObject
@property (nonatomic, strong) NSString *text;
@property (nonatomic, weak) PostOld *post; // 弱引用:可以为 nil
@end
@interface PostOld : NSObject
@property (nonatomic, strong) NSArray<CommentOld *> *comments;
@end
@implementation CommentOld
@end
@implementation PostOld
@end
// 块中的强捕获
void blockCaptureExample(void) {
Person *person = [[Person alloc] init];
person.name = @"Bob";
// 强捕获
void (^strongBlock)(void) = ^{
NSLog(@"%@", person.name); // 强捕获 person
};
// 弱捕获以避免循环引用
__weak Person *weakPerson = person;
void (^weakBlock)(void) = ^{
NSLog(@"%@", weakPerson.name); // 弱捕获
};
strongBlock();
weakBlock();
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
强引用会增加保留计数,而弱引用在对象释放时会自动设置为 nil,从而防止悬垂指针。
当对象彼此持有强引用时,会发生循环引用,从而阻止释放。打破循环需要使用弱引用或无主引用。
// 循环引用示例
@interface Parent : NSObject
@property (nonatomic, strong) NSArray<Child *> *children;
@end
@interface Child : NSObject
@property (nonatomic, weak) Parent *parent; // 弱引用以打破循环
@property (nonatomic, strong) NSString *name;
@end
@implementation Parent
- (void)dealloc {
NSLog(@"Parent deallocated");
}
@end
@implementation Child
- (void)dealloc {
NSLog(@"Child deallocated");
}
@end
void noCycleExample(void) {
Parent *parent = [[Parent alloc] init];
Child *child = [[Child alloc] init];
child.name = @"Alice";
child.parent = parent; // 弱引用
parent.children = @[child]; // 强引用
// 当 parent 超出作用域时,两者都会被释放
}
// 无循环的委托模式
@protocol DataSourceDelegate <NSObject>
- (void)dataSourceDidUpdate:(id)source;
@end
@interface DataSource : NSObject
@property (nonatomic, weak) id<DataSourceDelegate> delegate;
- (void)fetchData;
@end
@implementation DataSource
- (void)fetchData {
// 获取数据
[self.delegate dataSourceDidUpdate:self];
}
- (void)dealloc {
NSLog(@"DataSource deallocated");
}
@end
// 块的循环引用
@interface NetworkManager : NSObject
@property (nonatomic, strong) NSString *baseURL;
- (void)fetchDataWithCompletion:(void (^)(NSData *data))completion;
@end
@implementation NetworkManager
- (void)fetchDataWithCompletion:(void (^)(NSData *))completion {
// 模拟异步工作
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [@"response" dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
completion(data);
});
});
}
- (void)dealloc {
NSLog(@"NetworkManager deallocated");
}
@end
// 使用弱强舞避免块循环
@interface ViewController2 : UIViewController
@property (nonatomic, strong) NetworkManager *networkManager;
@end
@implementation ViewController2
- (void)loadData {
__weak typeof(self) weakSelf = self;
[self.networkManager fetchDataWithCompletion:^(NSData *data) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) return;
// 安全地使用 strongSelf
NSLog(@"Data loaded: %@", strongSelf.view);
}];
}
- (void)dealloc {
NSLog(@"ViewController2 deallocated");
}
@end
// 观察者模式循环
@interface Observable : NSObject
@property (nonatomic, strong) NSMutableArray *observers;
- (void)addObserver:(id)observer;
- (void)removeObserver:(id)observer;
- (void)notifyObservers;
@end
@implementation Observable
- (instancetype)init {
self = [super init];
if (self) {
_observers = [NSMutableArray array];
}
return self;
}
- (void)addObserver:(id)observer {
// 使用 NSPointerArray 进行弱引用
[self.observers addObject:[NSValue valueWithPointer:(__bridge void *)observer]];
}
- (void)removeObserver:(id)observer {
[self.observers removeObject:[NSValue valueWithPointer:(__bridge void *)observer]];
}
- (void)notifyObservers {
for (NSValue *value in self.observers) {
id observer = (__bridge id)(void *)[value pointerValue];
if (observer) {
// 通知观察者
}
}
}
@end
对于委托、父指针和观察者,始终使用弱引用来打破循环引用。在块中使用弱强舞来安全地访问 self。
ARC 提供了所有权限定符,用于显式控制变量和属性的内存管理行为。
// 属性所有权限定符
@interface Container : NSObject
// Strong:对象指针的默认值
@property (nonatomic, strong) id strongProperty;
// Weak:不阻止释放
@property (nonatomic, weak) id weakProperty;
// Copy:创建对象的副本
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *items;
// Assign:用于非对象类型
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) CGFloat value;
// Unsafe_unretained:弱引用但不置为 nil
@property (nonatomic, unsafe_unretained) id unsafeProperty;
@end
@implementation Container
@end
// 变量限定符
void qualifierExamples(void) {
// __strong:默认限定符
__strong NSString *strongString = @"Hello";
// __weak:弱引用
__weak NSString *weakString = strongString;
// __unsafe_unretained:非托管的弱引用
__unsafe_unretained NSString *unsafeString = strongString;
// __autoreleasing:用于输出参数
NSError * __autoreleasing error;
}
// Copy 属性行为
@interface Message : NSObject
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSArray *recipients;
@end
@implementation Message
@end
void copyPropertyExample(void) {
Message *message = [[Message alloc] init];
NSMutableString *mutableText = [NSMutableString stringWithString:@"Hello"];
message.text = mutableText; // 创建副本
[mutableText appendString:@" World"];
NSLog(@"Message text: %@", message.text); // 仍然是 "Hello"
NSLog(@"Mutable text: %@", mutableText); // "Hello World"
}
// 自动释放参数
BOOL loadData(NSData **outData, NSError **outError) {
if (outData) {
*outData = [@"data" dataUsingEncoding:NSUTF8StringEncoding];
}
return YES;
}
void autoreleaseExample(void) {
NSData *data;
NSError *error;
if (loadData(&data, &error)) {
NSLog(@"Loaded: %@", data);
} else {
NSLog(@"Error: %@", error);
}
}
// 属性属性组合
@interface ConfiguredObject : NSObject
// 只读强引用
@property (nonatomic, strong, readonly) NSString *identifier;
// 读写复制
@property (nonatomic, copy, readwrite) NSString *name;
// 可空弱引用
@property (nonatomic, weak, nullable) id<NSObject> delegate;
// 非空强引用
@property (nonatomic, strong, nonnull) NSArray *items;
@end
@implementation ConfiguredObject
@end
对于接受可变类型(如 NSMutableString 或 NSMutableArray)的属性,选择 copy 以防止意外的修改。
Core Foundation 对象需要显式内存管理,并需要桥接才能与 ARC 管理的 Objective-C 对象正确协作。
// 使用 __bridge 进行免费桥接
void bridgingExample(void) {
NSString *nsString = @"Hello";
// 桥接到 CF,不转移所有权
CFStringRef cfString = (__bridge CFStringRef)nsString;
// nsString 保留所有权
// 桥接回 NS
NSString *nsString2 = (__bridge NSString *)cfString;
}
// 将所有权转移到 CF
void bridgeRetainExample(void) {
NSString *nsString = @"Hello";
// 将所有权转移到 CF
CFStringRef cfString = (__bridge_retained CFStringRef)nsString;
// 必须手动 CFRelease
// 使用 cfString
CFIndex length = CFStringGetLength(cfString);
NSLog(@"Length: %ld", (long)length);
// 手动释放
CFRelease(cfString);
}
// 从 CF 转移所有权
void bridgeTransferExample(void) {
// 创建 CF 对象(拥有所有权)
CFMutableStringRef cfString = CFStringCreateMutable(NULL, 0);
CFStringAppend(cfString, CFSTR("Hello"));
// 转移到 ARC
NSMutableString *nsString = (__bridge_transfer NSMutableString *)cfString;
// ARC 现在拥有所有权,无需 CFRelease
[nsString appendString:@" World"];
NSLog(@"%@", nsString);
}
// 使用 CF 集合
void cfCollectionExample(void) {
// 创建 CF 数组
CFArrayRef cfArray = CFArrayCreate(
NULL,
(const void *[]){@"A", @"B", @"C"},
3,
&kCFTypeArrayCallBacks
);
// 桥接到 NSArray
NSArray *nsArray = (__bridge_transfer NSArray *)cfArray;
NSLog(@"Array: %@", nsArray);
}
// CF 属性列表
void cfPropertyListExample(void) {
NSDictionary *dict = @{@"key": @"value"};
// 转换为 CF
CFPropertyListRef plist = (__bridge_retained CFPropertyListRef)dict;
// 写入文件
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:@"/tmp/test.plist"];
CFPropertyListWrite(plist, url, kCFPropertyListXMLFormat_v1_0, 0, NULL);
CFRelease(plist);
}
// 处理 CF 回调
void myCFArrayApplierFunction(const void *value, void *context) {
NSString *string = (__bridge NSString *)value;
NSLog(@"Value: %@", string);
}
void cfCallbackExample(void) {
CFArrayRef cfArray = (__bridge CFArrayRef)@[@"A", @"B", @"C"];
CFArrayApplyFunction(
cfArray,
CFRangeMake(0, CFArrayGetCount(cfArray)),
myCFArrayApplierFunction,
NULL
);
}
使用 __bridge 进行临时桥接,使用 __bridge_retained 将所有权转移到 CF,使用 __bridge_transfer 将所有权从 CF 转移到 ARC。
当将 ARC 对象与 C 结构体混合使用时,需要对结构体中的对象指针进行显式内存管理。
// 包含对象指针的结构体
typedef struct {
__unsafe_unretained NSString *name;
NSInteger age;
} PersonStruct;
void structExample(void) {
PersonStruct person;
person.name = @"Alice"; // 必须使用 __unsafe_unretained
person.age = 30;
NSLog(@"Person: %@, %ld", person.name, (long)person.age);
}
// 更好的方法:使用 Objective-C 类
@interface PersonData : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation PersonData
@end
// 手动内存管理的结构体
typedef struct PersonStructManual {
CFStringRef name; // 使用 CF 类型进行保留所有权
NSInteger age;
} PersonStructManual;
PersonStructManual createPerson(NSString *name, NSInteger age) {
PersonStructManual person;
person.name = CFBridgingRetain(name); // 手动保留
person.age = age;
return person;
}
void releasePerson(PersonStructManual person) {
CFRelease(person.name); // 手动释放
}
void manualStructExample(void) {
PersonStructManual person = createPerson(@"Bob", 25);
// 使用 person
releasePerson(person);
}
// 结构体中的对象数组
typedef struct {
__unsafe_unretained NSArray *items;
NSInteger count;
} ContainerStruct;
// 替代方案:使用指向对象的指针
typedef struct {
void *items; // 需要时桥接到 NSArray
NSInteger count;
} ContainerStructPointer;
void pointerStructExample(void) {
NSArray *array = @[@1, @2, @3];
ContainerStructPointer container;
container.items = (__bridge void *)array;
container.count = array.count;
// 检索
NSArray *retrieved = (__bridge NSArray *)container.items;
NSLog(@"Items: %@", retrieved);
}
避免将 ARC 管理的对象存储在 C 结构体中。应使用 Objective-C 类或手动管理的 CF 类型。
自动释放池管理由便捷方法创建的临时对象,并防止循环中的内存累积。
// 主函数中的隐式自动释放池
int main(int argc, char *argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}
// 循环中的显式自动释放池
void processLargeDataset(void) {
NSArray *items = /* 大型数组 */;
for (id item in items) {
@autoreleasepool {
// 每次迭代释放临时对象
NSString *processed = [item description];
NSData *data = [processed dataUsingEncoding:NSUTF8StringEncoding];
// data 和 processed 在迭代结束时释放
}
}
}
// 没有自动释放池(内存累积)
void inefficientLoop(void) {
for (NSInteger i = 0; i < 1000000; i++) {
NSString *string = [NSString stringWithFormat:@"Number %ld", (long)i];
// string 直到外部池结束时才释放
}
}
// 有自动释放池(每次迭代释放内存)
void efficientLoop(void) {
for (NSInteger i = 0; i < 1000000; i++) {
@autoreleasepool {
NSString *string = [NSString stringWithFormat:@"Number %ld", (long)i];
// string 在每次迭代结束时释放
}
}
}
// 嵌套的自动释放池
void nestedPools(void) {
@autoreleasepool {
NSString *outer = @"outer";
@autoreleasepool {
NSString *inner = [NSString stringWithFormat:@"%@ inner", outer];
// inner 在内层池排空时释放
}
// outer 在外层池排空时释放
}
}
// 后台线程池
void backgroundWork(void) {
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
// 后台工作
NSString *result = [NSString stringWithFormat:@"Result"];
NSLog(@"%@", result);
}
});
}
在创建大量临时对象的紧密循环中使用显式自动释放池,以防止在自动释放池排空之间内存增长。
对委托和父引用使用弱引用,以打破常见委托和分层模式中的循环引用
在块中应用弱强舞,当捕获 self 时,防止循环引用,同时确保执行期间的安全访问
对可变类型属性选择复制,如 NSString 和 NSArray,以防止调用者意外修改
显式注解可空性,使用 nullable/nonnull 以提高 API 清晰度和 Swift 互操作性
在创建临时对象的循环中使用自动释放池,以防止长时间运行迭代中的内存累积
使用适当的限定符显式桥接 CF 类型,以管理 ARC 和手动引用计数之间的所有权转移
避免将对象存储在 C 结构体中,因为 ARC 无法管理它们;应使用 Objective-C 类或 CF 类型
在弱引用后检查 nil,因为当引用的对象释放时,它们可能随时变为 nil
对弱集合使用 NSPointerArray,当维护观察者或委托的集合时,以防止循环引用
使用 Instruments 进行分析,以检测循环引用、内存泄漏和过多的自动释放对象创建
使用强委托创建循环引用会导致内存泄漏;始终对委托属性使用弱引用
在块中强捕获 self而不使用弱引用,当块存储在属性中时会产生循环引用
在弱强舞中忘记强引用允许 self 在块执行期间释放
对父指针使用强引用会产生双向强引用并阻止释放
对 NSString 属性不使用复制允许调用者传递可变字符串并在之后修改它们
错误地桥接 CF 类型会导致过度释放或泄漏,具体取决于所有权转移方向
将 ARC 对象存储在 C 结构体中会导致过早释放或崩溃,因为 ARC 无法跟踪它们
创建 NSTimer 而不失效会强保留目标并阻止释放,直到失效
在紧密循环中缺少自动释放池会导致内存增长和内存压力导致的潜在崩溃
使用 unsafe_unretained 而不是 weak会产生悬垂指针,在释放后访问时会崩溃
在为 iOS、macOS、watchOS 或 tvOS 编写 Objective-C 代码时,使用 ARC 模式以确保内存安全的应用程序,而无需手动调用保留/释放。
在实现可能创建循环引用的委托、观察者或回调时,应用弱引用和弱强舞。
在与使用手动引用计数的 Core Foundation、Core Graphics 或其他基于 C 的框架交互时,采用适当的桥接。
在处理大型数据集、导入数据或在循环中创建许多临时对象的其他操作时,利用自动释放池。
在设计公共 API 时,使用适当的所有权限定符,以清晰地向客户端传达内存管理期望。
每周安装次数
–
代码仓库
GitHub 星标数
122
首次出现时间
–
安全审计
Automatic Reference Counting (ARC) is Objective-C's memory management system that automatically inserts retain and release calls at compile time. ARC eliminates most manual memory management while providing deterministic memory behavior and preventing common memory bugs like use-after-free and double-free.
Unlike garbage collection, ARC provides immediate deallocation when reference counts reach zero, making it suitable for resource-constrained environments like iOS. Understanding ARC's ownership rules, qualifiers, and patterns is essential for writing memory-safe Objective-C code and avoiding retain cycles.
This skill covers strong and weak references, ownership qualifiers, retain cycles, Core Foundation bridging, and best practices for ARC-based memory management.
Strong references maintain ownership of objects and prevent deallocation, while weak references observe objects without preventing deallocation.
// Strong references (default)
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *friends;
@property (nonatomic, strong) UIImage *photo;
@end
@implementation Person
@end
// Weak references
@interface ViewController : UIViewController
@property (nonatomic, weak) id<ViewControllerDelegate> delegate;
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@end
@implementation ViewController
@end
@protocol ViewControllerDelegate <NSObject>
- (void)viewControllerDidFinish:(ViewController *)controller;
@end
// Using strong and weak
void strongWeakExample(void) {
Person *person = [[Person alloc] init];
person.name = @"Alice"; // Strong reference to NSString
__weak Person *weakPerson = person; // Weak reference
NSLog(@"Weak person: %@", weakPerson.name);
person = nil; // Person deallocated
NSLog(@"After nil: %@", weakPerson); // weakPerson is now nil
}
// Unowned references (unsafe_unretained)
@interface NodeOld : NSObject
@property (nonatomic, unsafe_unretained) NodeOld *parent;
@property (nonatomic, strong) NSArray<NodeOld *> *children;
@end
@implementation NodeOld
@end
// Weak vs unowned
@interface CommentOld : NSObject
@property (nonatomic, strong) NSString *text;
@property (nonatomic, weak) PostOld *post; // Weak: can be nil
@end
@interface PostOld : NSObject
@property (nonatomic, strong) NSArray<CommentOld *> *comments;
@end
@implementation CommentOld
@end
@implementation PostOld
@end
// Strong captures in blocks
void blockCaptureExample(void) {
Person *person = [[Person alloc] init];
person.name = @"Bob";
// Strong capture
void (^strongBlock)(void) = ^{
NSLog(@"%@", person.name); // Captures person strongly
};
// Weak capture to avoid retain cycle
__weak Person *weakPerson = person;
void (^weakBlock)(void) = ^{
NSLog(@"%@", weakPerson.name); // Captures weakly
};
strongBlock();
weakBlock();
}
Strong references increment retain count, while weak references are automatically set to nil when the object deallocates, preventing dangling pointers.
Retain cycles occur when objects hold strong references to each other, preventing deallocation. Breaking cycles requires weak or unowned references.
// Retain cycle example
@interface Parent : NSObject
@property (nonatomic, strong) NSArray<Child *> *children;
@end
@interface Child : NSObject
@property (nonatomic, weak) Parent *parent; // Weak to break cycle
@property (nonatomic, strong) NSString *name;
@end
@implementation Parent
- (void)dealloc {
NSLog(@"Parent deallocated");
}
@end
@implementation Child
- (void)dealloc {
NSLog(@"Child deallocated");
}
@end
void noCycleExample(void) {
Parent *parent = [[Parent alloc] init];
Child *child = [[Child alloc] init];
child.name = @"Alice";
child.parent = parent; // Weak reference
parent.children = @[child]; // Strong reference
// When parent goes out of scope, both deallocate
}
// Delegate pattern without cycles
@protocol DataSourceDelegate <NSObject>
- (void)dataSourceDidUpdate:(id)source;
@end
@interface DataSource : NSObject
@property (nonatomic, weak) id<DataSourceDelegate> delegate;
- (void)fetchData;
@end
@implementation DataSource
- (void)fetchData {
// Fetch data
[self.delegate dataSourceDidUpdate:self];
}
- (void)dealloc {
NSLog(@"DataSource deallocated");
}
@end
// Block retain cycles
@interface NetworkManager : NSObject
@property (nonatomic, strong) NSString *baseURL;
- (void)fetchDataWithCompletion:(void (^)(NSData *data))completion;
@end
@implementation NetworkManager
- (void)fetchDataWithCompletion:(void (^)(NSData *))completion {
// Simulate async work
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [@"response" dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
completion(data);
});
});
}
- (void)dealloc {
NSLog(@"NetworkManager deallocated");
}
@end
// Avoiding block cycles with weak-strong dance
@interface ViewController2 : UIViewController
@property (nonatomic, strong) NetworkManager *networkManager;
@end
@implementation ViewController2
- (void)loadData {
__weak typeof(self) weakSelf = self;
[self.networkManager fetchDataWithCompletion:^(NSData *data) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) return;
// Use strongSelf safely
NSLog(@"Data loaded: %@", strongSelf.view);
}];
}
- (void)dealloc {
NSLog(@"ViewController2 deallocated");
}
@end
// Observer pattern cycles
@interface Observable : NSObject
@property (nonatomic, strong) NSMutableArray *observers;
- (void)addObserver:(id)observer;
- (void)removeObserver:(id)observer;
- (void)notifyObservers;
@end
@implementation Observable
- (instancetype)init {
self = [super init];
if (self) {
_observers = [NSMutableArray array];
}
return self;
}
- (void)addObserver:(id)observer {
// Use NSPointerArray for weak references
[self.observers addObject:[NSValue valueWithPointer:(__bridge void *)observer]];
}
- (void)removeObserver:(id)observer {
[self.observers removeObject:[NSValue valueWithPointer:(__bridge void *)observer]];
}
- (void)notifyObservers {
for (NSValue *value in self.observers) {
id observer = (__bridge id)(void *)[value pointerValue];
if (observer) {
// Notify observer
}
}
}
@end
Always use weak references for delegates, parent pointers, and observers to break retain cycles. Use the weak-strong dance in blocks for safe self access.
ARC provides ownership qualifiers that explicitly control memory management behavior for variables and properties.
// Property ownership qualifiers
@interface Container : NSObject
// Strong: default for object pointers
@property (nonatomic, strong) id strongProperty;
// Weak: doesn't prevent deallocation
@property (nonatomic, weak) id weakProperty;
// Copy: creates a copy of the object
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *items;
// Assign: for non-object types
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) CGFloat value;
// Unsafe_unretained: weak without nil-ing
@property (nonatomic, unsafe_unretained) id unsafeProperty;
@end
@implementation Container
@end
// Variable qualifiers
void qualifierExamples(void) {
// __strong: default qualifier
__strong NSString *strongString = @"Hello";
// __weak: weak reference
__weak NSString *weakString = strongString;
// __unsafe_unretained: unmanaged weak
__unsafe_unretained NSString *unsafeString = strongString;
// __autoreleasing: for out parameters
NSError * __autoreleasing error;
}
// Copy property behavior
@interface Message : NSObject
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSArray *recipients;
@end
@implementation Message
@end
void copyPropertyExample(void) {
Message *message = [[Message alloc] init];
NSMutableString *mutableText = [NSMutableString stringWithString:@"Hello"];
message.text = mutableText; // Copy made
[mutableText appendString:@" World"];
NSLog(@"Message text: %@", message.text); // Still "Hello"
NSLog(@"Mutable text: %@", mutableText); // "Hello World"
}
// Autoreleasing parameters
BOOL loadData(NSData **outData, NSError **outError) {
if (outData) {
*outData = [@"data" dataUsingEncoding:NSUTF8StringEncoding];
}
return YES;
}
void autoreleaseExample(void) {
NSData *data;
NSError *error;
if (loadData(&data, &error)) {
NSLog(@"Loaded: %@", data);
} else {
NSLog(@"Error: %@", error);
}
}
// Property attribute combinations
@interface ConfiguredObject : NSObject
// Read-only strong
@property (nonatomic, strong, readonly) NSString *identifier;
// Read-write copy
@property (nonatomic, copy, readwrite) NSString *name;
// Weak nullable
@property (nonatomic, weak, nullable) id<NSObject> delegate;
// Strong nonnull
@property (nonatomic, strong, nonnull) NSArray *items;
@end
@implementation ConfiguredObject
@end
Choose copy for properties that accept mutable types like NSMutableString or NSMutableArray to prevent unexpected mutations.
Core Foundation objects require explicit memory management and bridging to work correctly with ARC-managed Objective-C objects.
// Toll-free bridging with __bridge
void bridgingExample(void) {
NSString *nsString = @"Hello";
// Bridge to CF without transfer
CFStringRef cfString = (__bridge CFStringRef)nsString;
// nsString retains ownership
// Bridge back to NS
NSString *nsString2 = (__bridge NSString *)cfString;
}
// Transferring ownership to CF
void bridgeRetainExample(void) {
NSString *nsString = @"Hello";
// Transfer ownership to CF
CFStringRef cfString = (__bridge_retained CFStringRef)nsString;
// Must manually CFRelease
// Use cfString
CFIndex length = CFStringGetLength(cfString);
NSLog(@"Length: %ld", (long)length);
// Release manually
CFRelease(cfString);
}
// Transferring ownership from CF
void bridgeTransferExample(void) {
// Create CF object (owned)
CFMutableStringRef cfString = CFStringCreateMutable(NULL, 0);
CFStringAppend(cfString, CFSTR("Hello"));
// Transfer to ARC
NSMutableString *nsString = (__bridge_transfer NSMutableString *)cfString;
// ARC now owns, no CFRelease needed
[nsString appendString:@" World"];
NSLog(@"%@", nsString);
}
// Working with CF collections
void cfCollectionExample(void) {
// Create CF array
CFArrayRef cfArray = CFArrayCreate(
NULL,
(const void *[]){@"A", @"B", @"C"},
3,
&kCFTypeArrayCallBacks
);
// Bridge to NSArray
NSArray *nsArray = (__bridge_transfer NSArray *)cfArray;
NSLog(@"Array: %@", nsArray);
}
// CF property lists
void cfPropertyListExample(void) {
NSDictionary *dict = @{@"key": @"value"};
// Convert to CF
CFPropertyListRef plist = (__bridge_retained CFPropertyListRef)dict;
// Write to file
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:@"/tmp/test.plist"];
CFPropertyListWrite(plist, url, kCFPropertyListXMLFormat_v1_0, 0, NULL);
CFRelease(plist);
}
// Handling CF callbacks
void myCFArrayApplierFunction(const void *value, void *context) {
NSString *string = (__bridge NSString *)value;
NSLog(@"Value: %@", string);
}
void cfCallbackExample(void) {
CFArrayRef cfArray = (__bridge CFArrayRef)@[@"A", @"B", @"C"];
CFArrayApplyFunction(
cfArray,
CFRangeMake(0, CFArrayGetCount(cfArray)),
myCFArrayApplierFunction,
NULL
);
}
Use __bridge for temporary bridging, __bridge_retained when transferring to CF, and __bridge_transfer when transferring from CF to ARC.
When mixing ARC objects with C structures, explicit memory management is required for object pointers in structures.
// Struct with object pointers
typedef struct {
__unsafe_unretained NSString *name;
NSInteger age;
} PersonStruct;
void structExample(void) {
PersonStruct person;
person.name = @"Alice"; // Must use __unsafe_unretained
person.age = 30;
NSLog(@"Person: %@, %ld", person.name, (long)person.age);
}
// Better: Use Objective-C class instead
@interface PersonData : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation PersonData
@end
// Struct with manual memory management
typedef struct PersonStructManual {
CFStringRef name; // Use CF types for retained ownership
NSInteger age;
} PersonStructManual;
PersonStructManual createPerson(NSString *name, NSInteger age) {
PersonStructManual person;
person.name = CFBridgingRetain(name); // Retain manually
person.age = age;
return person;
}
void releasePerson(PersonStructManual person) {
CFRelease(person.name); // Release manually
}
void manualStructExample(void) {
PersonStructManual person = createPerson(@"Bob", 25);
// Use person
releasePerson(person);
}
// Arrays of objects in structs
typedef struct {
__unsafe_unretained NSArray *items;
NSInteger count;
} ContainerStruct;
// Alternative: Use pointer to object
typedef struct {
void *items; // Bridge to NSArray when needed
NSInteger count;
} ContainerStructPointer;
void pointerStructExample(void) {
NSArray *array = @[@1, @2, @3];
ContainerStructPointer container;
container.items = (__bridge void *)array;
container.count = array.count;
// Retrieve
NSArray *retrieved = (__bridge NSArray *)container.items;
NSLog(@"Items: %@", retrieved);
}
Avoid storing ARC-managed objects in C structures. Use Objective-C classes or CF types with manual management instead.
Autorelease pools manage temporary objects created by convenience methods and prevent memory buildup in loops.
// Implicit autorelease pool in main
int main(int argc, char *argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}
// Explicit autorelease pools in loops
void processLargeDataset(void) {
NSArray *items = /* large array */;
for (id item in items) {
@autoreleasepool {
// Temporary objects released each iteration
NSString *processed = [item description];
NSData *data = [processed dataUsingEncoding:NSUTF8StringEncoding];
// data and processed released at end of iteration
}
}
}
// Without autorelease pool (memory builds up)
void inefficientLoop(void) {
for (NSInteger i = 0; i < 1000000; i++) {
NSString *string = [NSString stringWithFormat:@"Number %ld", (long)i];
// string not released until end of outer pool
}
}
// With autorelease pool (memory released each iteration)
void efficientLoop(void) {
for (NSInteger i = 0; i < 1000000; i++) {
@autoreleasepool {
NSString *string = [NSString stringWithFormat:@"Number %ld", (long)i];
// string released at end of each iteration
}
}
}
// Nested autorelease pools
void nestedPools(void) {
@autoreleasepool {
NSString *outer = @"outer";
@autoreleasepool {
NSString *inner = [NSString stringWithFormat:@"%@ inner", outer];
// inner released when inner pool drains
}
// outer released when outer pool drains
}
}
// Background thread pools
void backgroundWork(void) {
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
// Background work
NSString *result = [NSString stringWithFormat:@"Result"];
NSLog(@"%@", result);
}
});
}
Use explicit autorelease pools in tight loops that create many temporary objects to prevent memory growth between autorelease pool drains.
Use weak for delegates and parent references to break retain cycles in common delegation and hierarchical patterns
Apply weak-strong dance in blocks when capturing self to prevent cycles while ensuring safe access during execution
Choose copy for mutable type properties like NSString and NSArray to prevent unexpected mutations by callers
Annotate nullability explicitly with nullable/nonnull to improve API clarity and Swift interoperability
Use autorelease pools in loops that create temporary objects to prevent memory buildup in long-running iterations
Bridge CF types explicitly with appropriate qualifiers to manage ownership transfer between ARC and manual reference counting
Avoid storing objects in C structs as ARC cannot manage them; use Objective-C classes or CF types instead
Check for nil after weak references as they can become nil at any time when the referenced object deallocates
Use NSPointerArray for weak collections when maintaining collections of observers or delegates to prevent cycles
Profile with Instruments to detect retain cycles, memory leaks, and excessive autoreleased object creation
Creating retain cycles with strong delegates causes memory leaks; always use weak for delegate properties
Capturing self strongly in blocks without weak creates cycles when blocks are stored in properties
Forgetting strong reference in weak-strong dance allows self to deallocate during block execution
Using strong for parent pointers creates bidirectional strong references and prevents deallocation
Not using copy for NSString properties allows callers to pass mutable strings and modify them later
Bridging CF types incorrectly causes over-releases or leaks depending on ownership transfer direction
Storing ARC objects in C structures leads to premature deallocation or crashes as ARC cannot track them
Creating NSTimer without invalidation retains target strongly and prevents deallocation until invalidated
Missing autorelease pools in tight loops causes memory growth and potential crashes from memory pressure
Using unsafe_unretained instead of weak creates dangling pointers that crash when accessed after deallocation
Use ARC patterns when writing Objective-C code for iOS, macOS, watchOS, or tvOS to ensure memory-safe applications without manual retain/release calls.
Apply weak references and the weak-strong dance when implementing delegates, observers, or callbacks that could create retain cycles.
Employ proper bridging when interfacing with Core Foundation, Core Graphics, or other C-based frameworks that use manual reference counting.
Leverage autorelease pools when processing large datasets, importing data, or performing other operations that create many temporary objects in loops.
Use appropriate ownership qualifiers when designing public APIs to clearly communicate memory management expectations to clients.
Weekly Installs
–
Repository
GitHub Stars
122
First Seen
–
Security Audits
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
109,600 周安装
Google Workspace CLI 模型响应安全清理工具 | Model Armor sanitize-response 命令使用指南
6,500 周安装
飞书日程待办摘要工作流:AI自动生成每日/每周开工报告,提升个人生产力
24,200 周安装
飞书会议纪要自动化汇总工作流:一键生成会议周报与纪要文档
24,000 周安装
lark-cli 共享规则:飞书资源操作指南与权限配置详解
24,000 周安装
Lark Calendar CLI 工具:智能日程管理与会议预约自动化命令行解决方案
24,000 周安装
OpenClaw 安全 Linux 云部署指南:私有优先、SSH隧道、Podman容器化
31,600 周安装