Objectif C - ARC - Quand utiliser @autoreleasepool

J'ai lu un peu sur l'ARC et j'ai vu ceci:

@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, lets 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" doesnt 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.

À la recherche à la présente partie;

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;
}

Quand il ne c = nil;
ne devrait pas c a n tous être détruits? Pourtant, il est dit que la sortie est seulement que ce n est détruit..
Quelqu'un peut m'expliquer pourquoi?

Et il dit que le résultat est aussi mauvais que d'une fuite de mémoire, alors comment voulez-vous résoudre ce problème?

Et une dernière question, quand devez-vous utiliser @autoreleaasepool?

Où étiez-vous lire ceci?
blog.webagesolutions.com

OriginalL'auteur user1021085 | 2012-02-11