Object-C memory

Source: Internet
Author: User
View code

 1   @ Interface Classc: nsobject {
2 @ Private
3
4 }
5 @ End
6
7 @ Implementation Classc
8
9 -( Void ) Dealloc
10 {
11 [Super dealloc];
12
13 }
14 @ End
15
16 @ Interface Classb: nsobject {
17 @ Private
18 Classc * class_c;
19 }
20
21 @ Property (nonatomic, retain) classc * classc;
22 @ End
23
24 @ Implementation Classb
25
26 @ Synthesize Classc = class_c;
27 -( ID ) Init
28 {
29 If (Self = [Super init]) {
30 Class_c = [[classc alloc] init];
31 }
32
33 Return Self;
34 }
35
36 -( Void ) Dealloc
37 {
38 [Super dealloc];
39 Self. classc = nil;
40 // [Class_c release];
41 }
42 @ End

The call is as follows:

 
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; classb * B = [[classb alloc] init]; [B release]; [pool release];}

 

Based on the debugging result, use self. classc = nil; you can also execute the dealloc method of classc. classc = nil & self. classc = nil & [class_c release] has the same effect.

If the classb init method does not initialize class_c and runs [class_c release], there will be no error because there is no problem in IOS in sending messages to an empty object.

 

In claasb

 
-(Void) dealloc {[Super dealloc]; // self. classc = nil; [class_c release]; // class_c = nil; [class_c release]; class_c = nil ;}

No problem.

 

However, the following issues occur in classb:

 @ interface classb: nsobject {@ private classc * class_c; classc * class_c1; classc * class_c2 ;} @ property (nonatomic, retain) classc * classc; @ property (nonatomic, retain) classc * classc1; @ property (nonatomic, retain) classc * classc2;-(void) test; @ end @ implementation classb @ synthesize classc = class_c; @ synthesize classc1 = class_c1; @ synthesize classc2 = class_c2;-(ID) Init {If (Self = [Super init]) {class_c = [[classc alloc] init]; class_c1 = [[classc alloc] init]; class_c2 = [[classc alloc] init];} return self;}-(void) test {self. classc = self. classc1; self. classc1 = self. classc2; self. classc2 = nil;}-(void) dealloc {[Super dealloc]; self. classc = nil; // = NIL is the same as self. classc1 = nil; self. classc2 = nil; // [class_c release]; // class_c = nil; // [class_c1 release]; // class_c1 = nil; /// [class_c2 release]; // class_c2 = nil ;}@ end 

In this way, the exc_bad_access error is reported during [B release];, which is a good alternative:

 
-(Void) dealloc {[Super dealloc]; // self. classc = nil; // self. classc1 = nil; // self. classc2 = nil; [class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil ;}

This time it is correct and there are no errors.Conclusion 1: When releasing an object, use [xxx release]; do not use self. XXXX = nil..

At this time, multiple calls are made.

[B test];
[B test];
[B test];
[B test];

There is no error. It is no problem to try again without initializing three class_c.

However, we also gotConclusion: Self. classc = nil & self. classc = nil & [class_c release] has the same effect.

are these two conclusions not contradictory? Why? I made the following attempt:

@ Interface classb: nsobject {@ private classc * class_c; classc * class_c1; classc * class_c2;} @ property (nonatomic, retain) classc * classc; @ property (nonatomic, retain) classc * classc1; @ property (nonatomic, retain) classc * classc2;-(void) test; @ end @ implementation classb @ synthesize classc = class_c; @ synthesize classc1 = class_c1; @ synthesize classc2 = class_c2;-(ID) Init {If (Self = [Super init]) {class_c = [[classc alloc] init]; class_c1 = [[classc alloc] init]; class_c2 = [[classc alloc] init];} return self;}-(void) test {nslog (@ "class_c is: % d ", class_c.retaincount); nslog (@" class_c1 is: % d ", class_c1.retaincount); nslog (@" class_c2 is: % d ", class_c2.retaincount); self. classc = self. classc1; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc1 = self. classc2; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc2 = nil; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount);}-(void) dealloc {[Super dealloc]; nslog (@" class_c is: % d ", class_c.retaincount); nslog (@" class_c1 is: % d ", class_c1.retaincount); nslog (@" class_c2 is: % d ", class_c2.retaincount); self. classc = nil; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc1 = nil; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc2 = nil; // [class_c release]; // class_c = nil; // [class_c1 release]; // class_c1 = nil; /// [class_c2 release]; // class_c2 = nil ;}

The results are the same, and they crash. In the dealloc method of classb, it's okay to try again. I tried again.

 
-(Void) dealloc {nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount ); nslog (@ "class_c2 is: % d", class_c2.retaincount); self. classc = nil; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc1 = nil; nslog (@ "class_c is: % d", class_c.retaincount); nslog (@ "class_c1 is: % d", class_c1.retaincount); nslog (@ "class_c2 is: % d ", class_c2.retaincount); self. classc2 = nil; [Super dealloc];}

At this time, there is no problem with the same god. The difference is [Super dealloc], which is correct in the future. Then we will perform the following two experiments:

-(Void) dealloc {[class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil; [Super dealloc];}

 

 
-(Void) dealloc {[Super dealloc]; [class_c release]; class_c = nil; [class_c1 release]; class_c1 = nil; [class_c2 release]; class_c2 = nil ;}

These two methods can be correctly executed. There is no problem, so the core of the problem is [Super delloc]. What is the impact on sekf. XXXX = nil?

 

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.