What I did is
int main(int argc, char *argv[]){
NSString *str = [[NSString alloc] init];
str = @"a a a a a a a a a ";
[str release];
NSLog(@"string is %@",str);
return 0;
}
I think string will be nil because we release it before printing it. However, it is not. Therefore, my consumption is wrong.
Can anybody advice me and correct me. Any comments are welcomed. Thanks
This declares a variable that is a pointer to an instance of
NSString, allocates memory for an instance ofNSString, initialises that memory, then assigns a pointer to that memory to the variable you declared.This creates an
NSStringconstant and assigns a pointer to it to the variable that you declared. As you no longer have a reference to the originalNSStringobject you created, you have leaked the memory you allocated for it.This declares that this code no longer wishes to be an owner of the memory allocated to
str. As you created it with a string literal, this object will always exist in your program, and releasing it does nothing.Releasing an object does not ever set it to
nil.