1

In the code below, alert was allocated and initialized, displayed, and then released. Before and after release, alert still points to the same address. Why does the system not set the alert pointer to nil after release?:

 -(void) viewDidLoad {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @”Hello”
                       message: @”This is an alert view”
                       delegate: self
                       cancelButtonTitle: @”OK”
                       otherButtonTitles: @”Option 1”, @”Option 2”, nil];
        [alert show];
        [alert release];
        [super viewDidLoad];
    }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
DungProton
  • 229
  • 3
  • 7

4 Answers4

3

I. Because even if it was appropriate, release can't (and doesn't) do anything to the object pointer it is called on, because, as anything in C, it's passed by value. Also, under manual reference counting, pointers are never set implicitly to nil, that's a feature of weak pointers under ARC.

II. And anyway it would be wrong. The UIAlertView class adds itself to some view as a subview when shown, so it gets retained. Therefore its reference count is not zero at the time you're expecting it to be deallocated.

1

Because the object that alert points to is still around. It is shown on screen, so it can't be deleted from memory.

Calling release only tells the system "I am done with it." It doesn't explicitly remove anything from memory until the system (and any other objects that hold a pointer to that object) is done with it too.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • You meaned release message didn't free memory immediately ? – DungProton Apr 10 '13 at 04:59
  • No it does not, @DungProton, as was [explained to you at length earlier](http://stackoverflow.com/questions/15908288/setter-method-really-do-in-obj-c). – jscs Apr 10 '13 at 05:02
  • yah, someone said release message free memory immediately but someone said no. That make me confused !!! – DungProton Apr 10 '13 at 05:05
  • 1
    At a high level, the object is no longer valid for you to use when you have released your last claim of ownership over it, @DungProton. That's all you should generally worry about; whether the memory is actually freed or not is irrelevant. At a lower level, when a `release` is sent that causes the object's reference count to transition to 0, the object is destroyed. You can, as Chuck indicated in your other question, look at the runtime library to see exactly what that means. – jscs Apr 10 '13 at 05:09
1

It's not possible for a message send like that to change the value of a pointer not local to it. You would have to do an assignment:

alert = [alert release];

Note that there are weak pointers available when you compile using ARC, which do indeed use the runtime library to nil out your variable when the pointed-to object is destroyed.

Secondly, release is not responsible for destroying the object. In many cases, the object is actually still alive after release is sent, because it has other owners.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • You mean `__weak UIAlertView * alert = [[UIAlertView alloc] initWithTitle:...` @Yar? Yes, that's legal, although the alert view can be deallocated immediately as far as ARC is concerned. – jscs Apr 10 '13 at 05:05
  • Thanks Josh, that's cool, I didn't know that. I'm trying to imagine in what situations, if any, that would be useful. – Dan Rosenstark Apr 10 '13 at 05:08
  • 1
    You're right, a local variable that auto-`nil`s probably has limited usefulness, @Yar, but if you ever need it, the possibility exists! – jscs Apr 10 '13 at 05:14
  • Thanks, I'll be on the lookout for a use-case ;) +1 on this answer, by the way. – Dan Rosenstark Apr 10 '13 at 12:43
  • 1
    I actually found a use-case today. Within your method, you are using an object (A) and modifying it. A belongs to some other object (B). If A gets dealloced during your use (because B is done with it) you want to be working with `nil` instead of 1) continuing to work with the object (as ARC would do normally) and 2) getting a SIGBART. – Dan Rosenstark Apr 10 '13 at 18:43
0

The pointer holds "the address of that thing". The release goes to "that thing". They're different memory locations.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42