-4

1.

NSMutableArray *content =[NSMutableArray new];

2.

NSMutableArray *content = [[NSMutableArray alloc] init];

in this what is the retain cout for(1), and (2).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Praveen
  • 3
  • 3
  • This Question has been asked multiple times. Have you googled it ? – Bhavin Jun 17 '13 at 07:15
  • The `new` method is shorthand for `alloc` and `init`. retain count is 1 for both. – Vinayak Kini Jun 17 '13 at 07:15
  • ok thanks for your answer, here i have small confusion. could u please tell me the retain cout for alloc and init. – Praveen Jun 17 '13 at 07:24
  • The retain count, as you should view it, is **+1**. Which means that you have retained one more time then you have released, and you must balance that with a **-1** (release) later. – borrrden Jun 17 '13 at 07:46

7 Answers7

2

Don't use retainCount[1]

You should never use -retainCount, because it never tells you anything useful. The implementation of the Foundation and AppKit/UIKit frameworks is opaque; you don't know what's being retained, why it's being retained, who's retaining it, when it was retained, and so on.-Dave DeLong

NSMutableArray *content =[NSMutableArray new]; //new will increase retainCount of content by 1

NSMutableArray *content = [[NSMutableArray alloc] init];//alloc will increase retainCount of content by 1
Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • *Don't use `retainCount`* followed by code with a comment making an absolute claim about the `retainCount`? O_o :) – bbum Jun 17 '13 at 07:53
1

Alloc : Class method of NSObject. Returns a new instance of the receiving class.

Init : Instance method of NSObject. Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

New : Class method of NSObject. Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.

retain count is 1 for both

Ravi
  • 888
  • 6
  • 24
1

The two expressions are identical in result. +new is just short hand for calling alloc and init. Literally, new is implemented as:

 + new { return [[self alloc] init]; }

Thus, both return an object with a retain count of +1. Not 1, but +1. That is, they return an object that your code must call release upon when your code is done with the object.

The absolute retain count is meaningless. Internally, NSMutableArray might retain the object 74 times and as long as it releases it 74 times as needed, that would be valid. Of course, NSMutableArray doesn't really play such shenanigans, but some classes do (especially the more complex classes that play games with the network and/or the UI).

bbum
  • 162,346
  • 23
  • 271
  • 359
  • <3 Thank you for the "+1, not 1" (reflects well on my comment)! I was so sad about all these answers saying "1" but I figured I wouldn't make a dent in them by posting another answer at this point. You surely will though! – borrrden Jun 17 '13 at 07:54
0

They are both 1. However you shouldn't be concerned about that. Remember:

If you allocate an object you are responsible for releasing it (I assume you are using MRR).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Only coincidentally; the implementation may be such that the RC is 5 after those lines of code. The absolute retain count is meaningless. – bbum Jun 17 '13 at 07:48
  • @bbum Agreed, hence the comment "you shouldn't be concerned about that". – trojanfoe Jun 17 '13 at 07:50
0

For (1) and (2), the retain count is 1, and unless you are using ARC be sure of releasing it somewhere.

IronMan
  • 1,505
  • 1
  • 12
  • 17
0

In both Array Retain count is 1

retainCount of an object changes in the following cases:

1) When you create an object(new or alloc, copy or mutablecopy), it has a retain count of 1.

2) When you send an object a retain message, its retain count is incremented by 1.

3) When you send an object a release message, its retain count is decremented by 1.

When you send a autorelease message to an object, its retain count will be decremented by 1(not immediately as in case of release but some time in the future)

Rushabh
  • 3,208
  • 5
  • 28
  • 51
  • You cannot make an absolute claim about the retain count. In both cases, there is one retain count that the code is responsible for if the object needs to be released. No more, no less. – bbum Jun 17 '13 at 07:47
0

The +new method is simply shorthand for +alloc and -init. The ownership semantics are identical. The only benefit to using +new is that it is more concise. If you need to provide arguments to the class's initialiser, you will have to use the +alloc and -initWith... methods instead. So the retain count will be 1 for both.

manujmv
  • 6,450
  • 1
  • 22
  • 35