4

a is an instance of NSString. I thought if I print a string after releasing it, it will crash the app. Instead it returned proper value assigned to it. My question is, shall we get the value of an object even after releasing it? If not, why I am able to see the value of a, even after it is deallocated?

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSString *a;
}

@end

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    a=[[NSString alloc]initWithString:@"abc"];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSLog(@"String %@",a);
    [a release];
    NSLog(@"release %@",a);
    [a retain];
    NSLog(@"retain %@",a); 
}

Output:-

2012-08-24 14:15:49.501 a[1176:f803] string abc
2012-08-24 14:15:53.404 a[1176:f803] release abc
2012-08-24 14:15:55.325 a[1176:f803] retain abc
5
  • I assume a is a property of your controller, how is it defined?
    – Asciiom
    Aug 24, 2012 at 8:57
  • you are not quite sure after the releasing you can access the object. it depends on when the memory of the original object will be overridden. when you are lucky you can access the object when you are unlucky the application is crashed. it is high risk to try to work with any object after you release them.
    – holex
    Aug 24, 2012 at 9:01
  • Thank You holex. Was just trying it out.. Thank u waldrumpus :)
    – Nina
    Aug 24, 2012 at 9:09
  • Also note the following implementation detail: passing a string literal as the argument to initWithString will return a pointer to the same string literal, since NSString is inmutable. String literals get allocated statically and never get deallocated.
    – albertamg
    Aug 24, 2012 at 9:11
  • 1
    Wow.. albertamg!!! i tried it out with NSMutableString and it crashes.. :)
    – Nina
    Aug 24, 2012 at 9:14

2 Answers 2

3

@"abc" is a constant, so it will never be released

feel the difference:

a = [[NSString alloc] initWithFormat:@"%d", 123];

it gives for me crash or release main output since it refers to some chunk of memory

2
  • Will it crash if I use a = [[NSString alloc] initWithFormat:@“%@", abc]; where abc is another string??
    – Nina
    Aug 24, 2012 at 9:35
  • @holex But it is mentioned here, stackoverflow.com/questions/5769769/…, It will stay in memory even after we release the NSString instance. Doesn’t that mean, its a constant? Or kinda that?
    – Nina
    Aug 24, 2012 at 9:45
3

Releasing any object means that the caller is done with it. After the release, the results of trying to access the object are undefined - could be a crash, could be that someone else is retaining the object and it works without crashing, could be something else entirely.

So, if you release an object, you should not try to access it afterwards in the same scope where you previously retained (init'ed, copied) it.

3
  • Thank you :) I was just hoping to see what happens when I release and retain immediately after that. I thought It will crash. And I tried printing the value after some delay, too. Even then it shows up the proper value.
    – Nina
    Aug 24, 2012 at 9:09
  • The retain/release mechanism is like a contract ensuring some things you may definitely do in your code, not defining the consequences for violating the contract.
    – waldrumpus
    Aug 24, 2012 at 9:17
  • 3
    Another example: Calling release on string literals (@"foo") will never cause them to be deleted.
    – waldrumpus
    Aug 24, 2012 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.