Small white Study Development (IOS) OC_ Manual memory Management (2015-08-02)

Source: Internet
Author: User

//
Main.m
Manual memory Management
//
Created by admin on 15/8/3.
Copyright (c) 2015 admin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[]) {

/*
When we create an object:
1. Allocating memory space, storing objects
2. Initializing member variables
3. Return the object's pointer address

1. When the object is created, a reference counter Retaincount is created internally, which is the only basis that the system uses to determine when the object is recycled, and when the object's reference counter Rataincount = 0 o'clock, the current object is immediately recycled. In fact, Retaincount is a property of each object and can be called
2. [Object Release] function is: retainCount-1
3. [Object retain] function is: Rataincount + 1
4. When the object's Retaincount = 0, the object is recycled and destroyed.
5. When an object is to be destroyed, the system automatically calls the Dealloc function to notify the object that it will be destroyed, as long as the function is to let the programmer know that the object has been destroyed, is a function of nsobject, can be rewritten to show that the object is recycled
6. Manual memory Management Principles (pairing principle): Whenever new, alloc, retain are present, there must be a release or autorelease
Note 7. Manual memory management focuses on two aspects of the problem:
① Wild Pointer operation
② memory leaks
*/
Here's the first practice of rewriting the DEALLOC function

Person *p = [[Person alloc]init]; Retaincount = 1;

NSLog (@ "%lu", P.retaincount);

[P retain]; Retaincount = 2;

NSLog (@ "%lu", P.retaincount);

[P release]; Retaincount = 1

NSLog (@ "%lu", P.retaincount);

[P release]; Retaincount = 0 was destroyed.

NSLog (@ "%lu", P.retaincount); Last step p This object has been destroyed, if it is called again through this destroyed object, it will be a wild pointer, there is an error
return 0;
}
//
Person.h
Manual memory Management
//
Created by admin on 15/8/3.
Copyright (c) 2015 admin. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person:nsobject

@end
//
Person.m
Manual memory Management
//
Created by admin on 15/8/3.
Copyright (c) 2015 admin. All rights reserved.
//

#import "Person.h"

@implementation person

-(void) dealloc
{
Be sure to call [Super Dealloc] to release related objects from the parent class before the object itself is destroyed.
[Super Dealloc];
NSLog (@ "person has been destroyed");
}
@end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Small white Study Development (IOS) OC_ Manual memory Management (2015-08-02)

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.