gpt4 book ai didi

objective-c - Objective-C - ARC - 何时使用@autoreleasepool

转载 作者:太空狗 更新时间:2023-10-30 03:23:56 26 4
gpt4 key购买 nike

我在阅读一些关于 ARC 的内容时看到了这个:

@interface Address : NSObject {
@public
NSString *city;
}
@end

@implementation Address
- (Address*) init: (NSString*) c {
city = c;

return self;
}

- (void) dealloc {
NSLog(@"Destroying address: %@", city);
}
@end

@interface Customer : NSObject {
NSString *name;
Address *addr;
}
@end

@implementation Customer
- (Customer*) init: (NSString*) n withAddress: (Address*) a {
//Note 1: Automatic retain on assignment
name = n;
addr = a;

return self;
}

- (void) dealloc {
NSLog(@"Destroying: %@", name);
//Note 2: Automatic release of member variables
}

@end

Customer* objectReturnTest() {
NSString * n = [[NSString alloc] initWithString: @"Billy Bob"];
Address * a = [[Address alloc] init: @"New York City"];

Customer *c = [[Customer alloc] init: n withAddress: a];

//Note 3: ARC will put the returned object in autorelease pool.
return c;
}

A couple of basic things to note here. As "Note 1" says, when an object is assigned to a variable, a call to retain is made automatically. This increments the reference count. As "Note 2" says, when an object is destroyed, all member variable objects are released for you. You no longer have to do that from the dealloc method.

Finally, when a method returns a newly created object, ARC will put the returned object in an autorelease pool. This is stated in "Note 3".

Now, let’s use the code.

int main (int argc, const char * argv[])
{
NSString * n = [[NSString alloc] initWithString: @"Johnny Walker"];
Address * a = [[Address alloc] init: @"Miami"];
Customer *c = [[Customer alloc] init: n withAddress: a];

NSLog(@"Before force release");
c = nil; //Force a release
NSLog(@"After force release");

@autoreleasepool {

Customer *c2 = objectReturnTest();

}
NSLog(@"After autorelease pool block.");

return 0;
}

The log output from this code will be:

Before force release

Destroying: Johnny Walker

After force release

Destroying: Billy Bob

Destroying address: New York City

After autorelease pool block.

Destroying address: Miami

A couple of things to note here. See how force release works. We set a variable to nil. ARC immediately releases the reference count. This causes the Customer object "Johnny Walker" to get destroyed. But, the member Address object "Miami" doesn’t get destroyed. This object gets destroyed at the very end of the main method. This is an extremely odd and non-intuitive behavior. Technically, this is not a memory leak, but, in reality member variables can pile up and take up a lot of memory. This is just as bad as memory leak.

The object return test works as expected. Customer "Billy Bob" is put in auto release pool. At the end of the @autoreleasepool block, the pool is drained and the object is released.

看这部分;

int main (int argc, const char * argv[])
{
NSString * n = [[NSString alloc] initWithString: @"Johnny Walker"];
Address * a = [[Address alloc] init: @"Miami"];
Customer *c = [[Customer alloc] init: n withAddress: a];

NSLog(@"Before force release");
c = nil; //Force a release
NSLog(@"After force release");

@autoreleasepool {

Customer *c2 = objectReturnTest();

}
NSLog(@"After autorelease pool block.");

return 0;
}

当他做 c = nil;c a 和 n 不应该全部销毁吗?然而它说输出只是 n 被销毁了..谁能解释一下为什么?

他说结果和内存泄漏一样糟糕,那你怎么解决呢?

最后一个问题,什么时候应该使用@autoreleaasepool?

最佳答案

按行

c = nil; //Forces a release

Customer 实例被释放,因为没有人保留它,因此输出是

Destroying: Johnny Walker

但是 na 还没有被释放,因为它们仍然在范围内并且 nil 还没有分配给它们。

而且我不认为这是任何类型的内存泄漏


你通常不需要使用@autorelasepool,除非你正在做这样的事情

- (void)myMethod {
for (int i = 0; i < 1000000; i++) {
NSString *string = [NSString stringWithFormat:@"%d", i];
// do something with string
}
}

超过 1000000 NSString 将在循环期间分配。它们将在方法返回后(实际上是在此运行循环之后)被释放,但已经消耗了太多内存。因此应该替换为

- (void)myMethod {
for (int i = 0; i < 1000000; i++) {
@autoreleasepool {
NSString *string = [NSString stringWithFormat:@"%d", i];
// do something with string
}
}
}

您应该阅读本文以了解有关内存管理的更多信息 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI

关于objective-c - Objective-C - ARC - 何时使用@autoreleasepool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9244735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com