0

Please explain me the below code of lines, I am just confused..,

Nsstring *a;
Nsstring *b;

a = [b retain];

what is the retain count of a & b.

a = [b copy];

what is the retain count of a & b.

Thanks in advance.

kubi
  • 48,104
  • 19
  • 94
  • 118
Santosh
  • 9
  • 3

2 Answers2

3

Technically the retain count in the situation you posted is indeterminate, since you never initialize your variables. Calling retain on an uninitialized pointer will probably crash.

Second, the retain count in your situation depends on how you init your variables.

NSString *a;
NSString *b = @"test";

a = [b retain];

/* Both variables reference the same object which has been retained.
   Retain count +1 
 */

NSString *a;
NSString *b = @"test 2";

a = [b copy];

/* `a` has a retain count +1 (a variable returned from a `copy`
   method has a retain count +1). `b` retain count does not change 
   (you haven't called `retain` on `b`, so it's count remains the
   same.
 */

If you haven't done so yet, you should read Apple's memory management guidelines. Also, unless you have a very good reason not to, you should be using ARC, which frees you from most of the headaches from manually managing memory.


In the comments on the other answer, you ask how to determine the retain count for an object. You always keep track of it yourself. Other objects may retain and release your string, but you don't care. If you create and object using alloc, call retain on an object or copy an object, you are responsible for releasing or autoreleasing that object when you are finished with it. Otherwise it isn't your responsibility. The absolute retain count of an object never matters.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • Thanks for the example and very gud explanation, i now know whaat i asked about. – Santosh Oct 17 '11 at 17:23
  • Is it important to release an object, if we don't release what would be the situation? – Santosh Oct 17 '11 at 17:37
  • 1
    @kubi Did you really run your program? `NSString` does a lot of crazy optimizations. The `retainCount` in your example all returns the max integer allowed in `NSUInteger`. That's another reason why you shouldn't take `retainCount` of any object. – Yuji Oct 17 '11 at 17:41
  • @Yuji. Yeah, you're right. I was using "retain count" in a more abstract way. – kubi Oct 17 '11 at 17:55
  • I see, but that abstract "retain count" is not in common use, I think, among Cocoa programmers. If I write an answer to this question, I just say "You don't. Forget about retain count." – Yuji Oct 17 '11 at 17:58
  • Scratch that. The only reason 'retainCount' Doesn't work here is because we're dealing with `NSString`s. For most objects, I *am* talking about retain counts. – kubi Oct 17 '11 at 18:18
  • 1
    If you were to switch to talking purely about the delta to the retain count, then it doesn't matter what goofy implementation details are in play... you worry about balancing your retains with releases and, beyond that, the retain count matters not. – bbum Oct 18 '11 at 17:32
-3

NSString doesn't have a retain count that will make sense. But if you're using as a general example, the way to find the retain count for objects that have a normal retain count is:

[a retainCount]
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 10
    You shouldn't ever use `retainCount`. It's an implementation detail that is almost never useful for anything practical. – kubi Oct 17 '11 at 17:09
  • 2
    And you should never call retainCount in a real program (it's a pretty sure sign you're doing something wrong.) – cobbal Oct 17 '11 at 17:10
  • @dani: So what is the retain count? and how to find the retain count ? – Santosh Oct 17 '11 at 17:13
  • 1
    I updated my answer, but in short: you don't care what the retain count is. – kubi Oct 17 '11 at 17:21
  • -1 `NSString` can very often have a retain count that makes some sense. – Dave DeLong Oct 17 '11 at 17:22
  • 3
    @Dani Its is very bad for debugging memory problems... Use zombies to debug those, not the retain count! – JustSid Oct 17 '11 at 17:34
  • @kubi That's exaggeration. I've been involved in a project that used custom weak reference implementation vitally based on `retainCount` (if `release` called when `retainCount` is 1, all weak references become nil). That has been working like a charm for years from 10.1 thru 10.7, was thread-safe, and those weak references were sure useful a lot. – hamstergene Oct 17 '11 at 17:52
  • I said "almost never useful". I doubt @Santosh is asking this question because he wants to implement his own weak reference code. – kubi Oct 17 '11 at 17:59
  • I +1'd this. `retainCount` is well-documented and *required* method of NSObject protocol (i.e. anything that pretends to be a NSObject *must* implement it). It is also documented what should be done when the object is never released, like some NSString-s for example. – hamstergene Oct 17 '11 at 17:59
  • 2
    @Dani: Looking at retain counts is *not* very good for debugging, because they give you a horribly incomplete and quite possibly misleading picture of an object's memory management. If you need to look at retain counts, it's usually because you're doing something else wrong. The documentation on `retainCount` even includes a large note that begins: "IMPORTANT: This method is typically of no value in debugging memory management issues." – Chuck Oct 17 '11 at 18:03
  • 1
    The only time I've *ever* used `retainCount` is indirectly via Instruments. With few exceptions (like @hamstergene's code) it is of little use and has wasted tens of gigabytes of Stackoverflow's bandwidth. – kubi Oct 17 '11 at 18:21
  • 1
    @hamstergene's use case is an advanced pattern and, while useful, is oft very fragile. Implementing caching semantics on top of `retainCount` is begging for trouble (and has been the source of many many bugs that I've debugged over the years). Can be done and it is one of the very few legitimate uses, but to be avoided. – bbum Oct 18 '11 at 17:31
  • Can't argue with that. And speaking about this question, using `retainCount` for experimentation (for the purpose of studying how reference counting works) is another example of legitimate use, isn't it. – hamstergene Oct 18 '11 at 19:24
  • 1
    It really isn't very good for learning how reference counting works, either. At least, not unless you limit it to your own class and don't ever pass instances through the frameworks. You are far better off firing up the Allocations instrument, turning on reference count tracking, and looking at the absolute retain count in *that* context as it'll show both the count and exactly where it was increased/decreased. – bbum Oct 19 '11 at 20:27