Small white Study Development (IOS) OC_ memory management for individual objects (2015-08-02)

Source: Internet
Author: User

//
Main.m
Memory management for a single object
//
Created by admin on 15/8/3.
Copyright (c) 2015 admin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
void Test ();
void Test5 (person * p);
int main (int argc, const char * argv[]) {

Test ();
Person *p = [[Person alloc]init];
TEST5 (P);
[P release];
NSLog (@ "%lu", P.retaincount);
[P release];

return 0;
}

void Test ()
{
Person *p = [[Person alloc]init]; Retaincount = 1
P.age = 10;
NSLog (@ "%@", p);
[P release]; Retaincount = 0 object is recycled

[P run];//The method of invoking an object through an object that has been reclaimed, and this pointer is called a wild pointer at the moment, so the call is wrong
/*
Attention:
The system has recycled the object that P points to
Exc_bad_access access to inaccessible memory space
Objects that are reclaimed by the system we call them zombie objects.
By default, Xcode does not check zombie objects in real time to improve encoding efficiency
*/

/*
Method: In order to prevent the operation of the wild pointer, when we determine that the object in the current scope is no longer being used, we usually assign the pointer variable that we no longer use to nil
*/
p = nil;

P.age = 30; Then these two sentences indicate: Nil.age = 30;
[P run]; [Nil Run]
}



void Test2 ()
{
/*
Memory leaks: Memory leaks refer to objects that are no longer being used and are not being recycled in memory
That is: As long as the object Retaincount! = 0 So this object is always in memory.
*/
Analysis of two types of memory leaks:
The first case of a memory leak:
Person *p = ["Person Alloc]init];//retaincount = 1
P.age = 10;
[P retain]; Retaincount = 2
[P retain]; Retaincount = 3
[P release];//retaincount = 2
[P release];//retaincount = 1
The object's Retaincount! = 0 object is not destroyed, i.e. the object is always in memory
}
void Test3 ()
{
Person *p = [[Person alloc]init];
P.age = 90;

The second case of a memory leak:
[P run];

p = nil;
[P release]; This only assigns a pointer to the object to nil, and here is [nil release], in fact the object is still in memory and is not released, and there is no pointer pointing to the object at this time.
}
void Test4 ()
{
Person *p = [[Person alloc]init];
P.age = 80;

Person *P2 = p;
[P release]; The object has been destroyed here.
[P2 run]; There are pointers, but no objects, so here is the wild pointer operation
}
void Test5 (person * p)
{
P.age = 50;
NSLog (@ "%@", p);
[P retain];
NSLog (@ "%lu", P.retaincount);

}

//
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

@property int age;
-(void) run;

@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
{
[Super Dealloc];
NSLog (@ "object has been destroyed");
}

-(NSString *) description
{
return [NSString stringwithformat:@ "age =%d", _age];
}

-(void) run
{
NSLog (@ "People running up");
}

@end

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

Small white Study Development (IOS) OC_ memory management for individual objects (2015-08-02)

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.