1

I am having trouble understanding why NSLog reports "dog" when the code is run. I understand about retain counts and dealloc e.t.c. What simple thing am i missing ?

NSString *newFoo = @"dog";
 [newFoo release];
 NSLog(newFoo);
Jon B
  • 51,025
  • 31
  • 133
  • 161
Ben Chester
  • 129
  • 4
  • 12
  • Thanks for the answer. Im just trying to get to grips with Objective-c and i was writing this to see if i understood what was going on. – Ben Chester Sep 02 '09 at 18:53
  • 1
    Why are there so many people saying "I don't know Objective-C but" and then making assumptions about its implementation? If you don't the answer for sure, there's no point in answering. The button says "Post your answer", not "Post your guess" or "Post your assumption". – dreamlax Sep 03 '09 at 00:06

5 Answers5

7
[@"String Literal" release];

is a noop;

NSString *literal = @"String Literal";
[literal release];

is also a noop. This is only the case for string literals; and you should never expect this behaviour anywhere else. (This is to say; even though you tell the object to release, it just doesn't.)

Randolpho
  • 55,384
  • 17
  • 145
  • 179
Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • Exactly, although you may have stated it more clearly than I did. – Gregory Higley Sep 02 '09 at 18:53
  • 2
    And you should certainly never consider this behavior when programming. The retain count is an implementation detail, the only thing you should care about is balancing your retains with your releases. – bbum Sep 02 '09 at 22:06
5

I believe it's because @"dog" is effectively treated as a constant by the compiler. It creates some subclass of NSString (which is a class cluster) which persists for the lifetime of the application.

Just discovered this question for the definitive answer, which is essentially the same as mine.

Community
  • 1
  • 1
Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
2

A String Literal is a special case.. http://thaesofereode.info/clocFAQ/

But in general assuming you are not using garbage collection just stick to the few simple rules..

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

more pertinently.. retain an object when you need it to stick around - balance it with a release when done. If you didn't create or retain it, you don't need to release it.

Sending -release to an object isn't like freeing the memory, it only signals that you are done with it. It may well still be around or it may not. You have no way of knowing, you don't need to know, and you shouldn't try to find out if it is used elsewhere, if clever apple code has decided to cache it, if it's a singleton, etc.

It may well still be valid but when you have sent release you have effectively said "I'm done" and not "reclaim this memory".

-1

You are de-allocating the chunk of memory the pointer's pointing at, but the data is still there.

Releasing it doesn't automatically zeroes out that part of the memory.

So you can still read the data out just fine but it's just might be snapped (allocated) by something else down the line... but just not yet.

I'm not an ObjC coder by trade but as since its compatible with C that's I'm guessing from my C experience.

chakrit
  • 61,017
  • 25
  • 133
  • 162
  • Only by a special case for string literals. Change that to any other class and any other initialization scheme and chakrit is 100% correct. – Randolpho Sep 02 '09 at 19:49
  • One flaw and you get a flurry of downvotes. Guess that's what "community" means. – chakrit Sep 04 '09 at 04:44
-1

Although the object is probably released (not sure how Obj-C handles strings internally, some languages basically cache strings), you have not writted anything new in the memory of newFoo after you released it. The memory where the string was stored keeps its content, but could be overwritten at any time.

newFoo still points to the same adress in memory, but that memory could become anything else at any moment.

The way to really be sure that you don't point to potentially dirt memory is to set newFoo to nil after you released it.

mrueg
  • 8,185
  • 4
  • 44
  • 66