@property (nonatomic, weak) id a;
@property (nonatomic, weak) id sa;
I have two weak properties. The array will be release when after copy. But the NSString not be released. I don't know the difference. Why the string not be released?
Look the code and the output:
{
NSMutableString *sa = [[NSMutableString alloc] initWithString:@"sa"];
NSMutableArray *array = [NSMutableArray arrayWithObject:@"aaa"];
self.a = [array copy];
self.sa = [sa copy];
NSLog(@"array:%p", array);
NSLog(@"self.a:%p", self.a);
NSLog(@"self.sa:%p", self.sa);
}
array:0x6000000479b0
self.a:0x0
self.sa:0xa000000000061732
What happens is that your string copy is not an actual object, but you get a tagged pointer. Basically the whole string is stored inside the pointer and there is no allocation at all. And where there is no allocation there is no memory to be freed, and so the weak reference cannot get reset to nil
.
If you look at the actual pointer 0x617325
value and split it into bytes you find your whole string:
0x25 - Length (2) + flag (5) telling that it is a tagged string
0x73 - 's'
0x61 - 'a'
If this was a real pointer the last digit would have to be a zero due to alignment requirements. This is the whole magic that makes those tagged pointers possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With