C * c2 = [[C alloc]init];
c2.test1 = @"whatever";
NSLog(@"%@",c2.test1);
[c2 release];
NSLog(@"%lu",(unsigned long)c2.retainCount);
[c2 release];
NSLog(@"%@",c2.test1);
print the results :
2013-09-15 15:29:36.936 MemoryTest [1529:707] whatever
2013-09-15 15:29:36.938 MemoryTest [1529:707] delloc C
2013-09-15 15:29:36.939 MemoryTest [1529:707] 1
2013-09-15 15:29:36.939 MemoryTest [1529:707] whatever
why he was not being given ah , c2 is not released yet ? ? ? And dealloc method has been called the , ah, had destroyed ah ? ? ?
everyone from the perspective of the heap and stack explain it to me ? ? ? My understanding is that c2 is also still pointing to the original pointer that fast memory on the heap , the heap memory contents have not changed, but is marked as available, that can always be covered.
C class is so
@implementation C
@synthesize test1;
-(void)dealloc{
NSLog(@"delloc C");
[test1 release];
[super dealloc];
}
@end
------ Solution ------------------------------------- -------
About the message sent to released object Can you give an example? ? ?
because your program is very short, so c2 dealloc abandoned after the value of that part of the memory is old , no one to use it.
And when your program are somewhat complex, and multi- threading may occur when c2 memory pointed to just dealloc immediately re- assigned , for example, this time c2 memory pointed to an NSData, this when you go to call methods such stringByAppendingString NSString will complain because NSData without this approach, and you think c2 points to NSString, error type is Unexcepted selector send to instance: 0x1290a1 this .
Basically you use NSLog error does not occur , because all subclasses of NSObject NSLog can be used to output the description information . Even without also output memory address and the class name.
And you said no oc this error : message sent to released object
you are prepared , right ?
------ eference -------------------------------------- -
you think is right, because c2 is a pointer to a memory region recovered re-write data is not being used , so the memory block is still the old value.
------ eference --------------------------------------- < br>
However, if this time is not given, then what will appear when the message sent to released object such a mistake? ? ? ?
------ eference --------------------------------------- < br>
About the message sent to released object Can you give an example? ? ?
------ eference --------------------------------------- < br>
About the message sent to released object Can you give an example? ? ?
refer to this , enable the Enable Zombie Objects: http://blog.csdn.net/lydyangliu/article/details/9035863
If you follow normal use C / C + + logical point of view, this time , the program will throw exception .
Of course, good programming habits , for pointer processing, natural is the judgment of non- empty , exhausted released promptly set nullptr
------ eference ---------- -----------------------------
About the message sent to released object Can you give an example? ? ?
because your program is very short, so c2 dealloc abandoned after the value of that part of the memory is old , no one to use it.
And when your program are somewhat complex, and multi- threading may occur when c2 memory pointed to just dealloc immediately re- assigned , for example, this time c2 memory pointed to an NSData, this when you go to call methods such stringByAppendingString NSString will complain because NSData without this approach, and you think c2 points to NSString, error type is Unexcepted selector send to instance: 0x1290a1 this .
Basically you use NSLog error does not occur , because all subclasses of NSObject NSLog can be used to output the description information . Even without also output memory address and the class name.
And you said no oc this error : message sent to released object
you are prepared , right ?
wrong that mistake , Khan . . . . . Is the message sent to dealloced object, ( hope I did not misspell ) , I asked , this error and Unexcepted selector send to instance where the difference between ? ? ? What will be reported when the first error ? ? ?
------ eference --------------------------------------- < br>
About the message sent to released object Can you give an example? ? ?
because your program is very short, so c2 dealloc abandoned after the value of that part of the memory is old , no one to use it.
And when your program are somewhat complex, and multi- threading may occur when c2 memory pointed to just dealloc immediately re- assigned , for example, this time c2 memory pointed to an NSData, this when you go to call methods such stringByAppendingString NSString will complain because NSData without this approach, and you think c2 points to NSString, error type is Unexcepted selector send to instance: 0x1290a1 this .
Basically you use NSLog error does not occur , because all subclasses of NSObject NSLog can be used to output the description information . Even without also output memory address and the class name.
And you said no oc this error : message sent to released object
you are prepared , right ?
wrong that mistake , Khan . . . . . Is the message sent to dealloced object, ( hope I did not misspell ) , I asked , this error and Unexcepted selector send to instance where the difference between ? ? ? What will be reported when the first error ? ? ?
an object can not be found , one can not find the way
------ eference ----------------------- ----------------
release is only marked void, can be destroyed .
As for when destroyed , depending on the current memory usage and destruction rules .
seemingly discussed inside the post two years ago .
------ eference --------------------------------------- < br> this problem also plagued me !
------ eference --------------------------------------- < br> release is slow compared to some dealloc , release should be first in the queue before destroying the object ( or stack ) , remember, is almost like , finally calling dealloc to destroy . . .
没有评论:
发表评论