【发布时间】:2012-11-19 17:00:24
【问题描述】:
我有一个关于 xcode ARC 的初学者问题。以下代码没有内存问题,因为内存已被 ARC 释放。
- (void)viewDidLoad
{
[super viewDidLoad];
// test nsmutabledata
dispatch_queue_t testQueue = dispatch_queue_create("testQueue", NULL);
dispatch_async(testQueue, ^{
while (1) {
NSMutableData *testData = [[NSMutableData alloc]initWithCapacity:1024*1024*5];
NSLog(@"testData size: %d", testData.length);
}
});
}
但是,下面没有,几秒钟后给我内存分配错误。
+ (NSMutableData *) testDataMethod
{
NSMutableData *testDataLocal = [[NSMutableData alloc]initWithCapacity:1024*1024*5];
return testDataLocal;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// test nsmutabledata
dispatch_queue_t testQueue = dispatch_queue_create("testQueue", NULL);
dispatch_async(testQueue, ^{
while (1) {
NSMutableData *testData = [RootViewController testDataMethod];
NSLog(@"testData size: %d", testData.length);
}
});
}
我对 ARC 有错误的理解吗?我虽然 testDataLocal 被计算一次,但在方法退出时超出了范围。 testData 是另一个计数,但在循环的下一次迭代中 testData 应该没有计数,并被系统释放。
【问题讨论】:
标签: objective-c ios memory-management automatic-ref-counting grand-central-dispatch