As far as I know, NSString is “created once and readonly” type. When reassigning value to a NSString, we in fact change the pointer’s value making it point to another memory address, but the NSString object remains unchanged.
My question is: if the NSString object has no other pointers pointing to it, is that causing a memory leak after reassigning?
To discuss in details, please read the following code:
@interface ViewController ()
@property (nonatomic,strong) NSString* aString;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.aString = [NSString stringWithFormat:@"Hello %@",@"Philip"];
// only for setting a break point
int x;
// reassigning
self.aString = [NSString stringWithFormat:@"Goodbye"];
// only for setting another break point
int y;
}
@end
In debug console:
(lldb) po self.aString
(NSString *) $0 = 0x00337d00 Hello Philip
(lldb) po self.aString
(NSString *) $1 = 0x3f41dfe0 Goodbye
(lldb)
It clearly shows that after reassigning value, aString now points to a different memory location. My another question is: how can I display the object that starts at 0x00337d00 in debug console?
If the NSString has no pointer to it, it should get dealloc’ed. That may only happen after the autorelease pool is drained thought, since it was created with a method that should return it autoreleased.
Also literal NSStrings like
@"string"never get dealloc’ed, since they are actually constants (somewhat like singletons).