Monday, January 13, 2014

The release on ios6

I read this article:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html # / / apple_ref/doc/uid/TP40004447-1000810

There are so many words:
references
Collections Own the Objects They Contain

When you add an object to a collection (such as an array, dictionary, or set), the collection takes ownership of it. The collection will relinquish ownership when the object is removed from the collection or when the collection is itself released. Thus, for example, if you want to create an array of numbers you might do either of the following:

NSMutableArray * array = <# Get a mutable array #>;
NSUInteger i;
/ / ...
for (i = 0; i <10; i + +) {
NSNumber * convenienceNumber = [NSNumber numberWithInteger: i];
[Array addObject: convenienceNumber];
}
In this case, you didn't invoke alloc, so there's no need to call release. There is no need to retain the new numbers (convenienceNumber), since the array will do so.

NSMutableArray * array = <# Get a mutable array #>;
NSUInteger i;
/ / ...
for (i = 0; i <10; i + +) {
NSNumber * allocedNumber = [[NSNumber alloc] initWithInteger: i];
[Array addObject: allocedNumber];
[AllocedNumber release];
}
In this case, you do need to send allocedNumber a release message within the scope of the for loop to balance the alloc Since the array retained the number when it was added by addObject:., It will not be deallocated while it's in the array.

To understand this, put yourself in the position of the person who implemented the collection class. You want to make sure that no objects you're given to look after disappear out from under you, so you send them a retain message as they're passed in. If they're removed, you have to send a balancing release message, and any remaining objects should be sent a release message during your own dealloc method.


But I wrote a little program in IOS6 in,
 NSMutableArray * array = [NSMutableArray array] 

for (i = 0; i <10; i + +) {
NSNumber * allocedNumber = [[NSNumber alloc] initWithInteger: i];
[Array addObject: allocedNumber];
}


No need to call
 autorelease 
or
 release 
, seeking explain.
Reply:
Your project is ARC, right? ARC, then do not call.
Reply:
cited a floor dream238 reply:
your project is ARC, right? ARC, then do not call.


Is not in ios6, if you use ARC, the program will not need to consider ape memory management thing.
Reply:
reference to the second floor wangyangkobe reply:
is not in ios6, if you use the ARC, the program will not need to consider ape memory management thing.


Yes, but not if you use ARC, problems of memory, the investigation would be more trouble ...
Reply:
reference to the third floor dream238 reply:
Quote: references to the second floor wangyangkobe reply:

Is not in ios6, if you use ARC, the program will not need to consider ape memory management thing.


Yes, but not if you use ARC, problems of memory, the investigation would be more trouble ...


Okay, thank you, you have nothing to use the experience?

Reply:
references, 4th Floor wangyangkobe reply:
good, thank you, then you have nothing to use the experience?


ARC not used, used manual control. ARC works before watching colleague Richard memory problems, waste a lot of efforts.

No comments:

Post a Comment