Releasing objects in MRR is implemented through release or autorelease messages, which immediately frees reference count-1, sending autorelease messages to put objects in a memory-free pool for deferred release, and the object's reference count does not really change. Instead, a record is added to the memory-free pool until all objects in the pool are notified to send the release message and the reference count is actually reduced before the pool is destroyed.
Because it will delay the release of the object, do not use Autorelease to release the object, unless it is necessary, the release of the default memory release pool in the iOS program is at the end of the program, the application entry MAIN.M code:
int main (int argc, char *argv[])
{
@autoreleasepool {return
uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));
}
Code is wrapped in @autoreleasepool {...} Between, which is the scope of the pool, the default is the entire application. A memory leak can also occur if a large number of objects are generated with Autorelease release. So when does autorelease have to be? Let's look at the following code:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
static NSString *cellidentifier = @ "Cellidentifier";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:celli
Dentifier] autorelease];
Nsuinteger row = [Indexpath row];
Nsdictionary *rowdict = [Self.listteams objectatindex:row];
Cell.textLabel.text = [rowdict objectforkey:@ "name"];
NSString *imagepath = [rowdict objectforkey:@ "image"];
ImagePath = [ImagePath stringbyappendingstring:@ ". png"];
Cell.imageView.image = [UIImage Imagenamed:imagepath];
Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;
return cell; }
The cell object cannot be immediately release, and we need to use it to set the table view screen. Autorelease is generally applied to methods that provide objects to other callers, where the object cannot be released immediately, and needs to be deferred.
In addition, there is a situation where the autorelease is used, which is referred to as the "class-level construction method":
NSString *message = [NSString stringwithformat:@ "You have selected the%@ team. ", Rowvalue];
The ownership of this object is not the current caller, but it is put into the pool by the iOS system by sending autorelease messages, which are not visible to developers, and we should also be careful to reduce the use of such statements.
This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1132754