Automatic release in iOS?

Source: Internet
Author: User

-- Preface: although I know the rules for releasing objects in iOS development, I am not sure when the automatically released objects will disappear completely? How long does it exist? What will disappear? I don't know. Every time I release objects automatically, I feel a little confused. Here are some examples and research.

-- Directly write the code in the viewDidLoad method of a controller (the Code content is to use a path to save a data, but the path is to automatically release the object ):

[Html]
Example 1
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: path atomically: NO];

// Example 1
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: path atomically: NO]; example 1. If this is the case, the file can be saved. [Html] view plaincopyprint? // Example 2
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];
// Call the method. The path is used as the parameter.
[Self saveData: path];
// Split line
-(Void) saveData :( NSString *) filePath
{
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: filePath atomically: NO];
}

// Example 2
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];
// Call the method. The path is used as the parameter.
[Self saveData: path];
// Split line
-(Void) saveData :( NSString *) filePath
{
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: filePath atomically: NO];
} Example 2: It's okay to write this. The file can be saved. Or [self saveData: path]; change it to [self saveData]; The saveData method does not input parameters, and uses the member variable path to replace the filePath function. It is okay to write it like this. [Html] view plaincopyprint? // Example 3
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];

// Create button
UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (0.0, 0.0, 100,100)];
Button. backgroundColor = [UIColor redColor];
[Button addTarget: self action: @ selector (saveData) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
-(Void) saveData
{
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: path atomically: NO];
}

// Example 3
NSArray * patharray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docstr = [patharray objectAtIndex: 0];
// Path member variable, file path-automatically release object
Path = [docstr stringByAppendingPathComponent: @ "1.png"];

// Create button
UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (0.0, 0.0, 100,100)];
Button. backgroundColor = [UIColor redColor];
[Button addTarget: self action: @ selector (saveData) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
-(Void) saveData
{
// Read data
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "IMG_1340" ofType: @ "PNG"];
NSData * data = [NSData dataWithContentsOfFile: imagePath];
// Use path to save data
[Data writeToFile: path atomically: NO];
} In Example 3, click the button and the program will crash, prompting "Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0000008 )". However, if you change path = [docstr stringByAppendingPathComponent: @ "1.png"]; To path = [NSString stringWithString: [docstr stringByAppendingPathComponent: @" 1.png"]; the program does not crash.
-- To sum up, the existence cycle of "automatic release object returned by the System" is valid in one method (example 1/2), but it is removed from this method. In other methods, it cannot be used elsewhere (Example 3). At the same time, "automatically released by yourself" is longer than "automatically released objects returned by the system" (example 3 is changed ), but it does not always exist. Sometimes it is useless when it is used. (once upon a time, a self-created automatically released array has been running for a long time and sometimes crashed, that is, the array is not used ).

-- To sum up, if you want to use an object, you can release the object message after retain is used up. This ensures that the object message is automatically released. (In Example 3, change path = [docstr stringByAppendingPathComponent: @ "1.png"]; To path = [NSString stringWithString: [docstr stringByAppendingPathComponent: @" 1.png"], and the program runs properly)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.