Questions tagged [retaincount]

-retainCount is a commonly misused method related to memory management of objects in the Cocoa and Cocoa Touch frameworks. It should never be used.

Quoting the NSObject documentation:

This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

To understand the fundamental rules of memory management that you must abide by, read “Memory Management Policy”. To diagnose memory management problems, use a suitable tool:

  • The Clang Static analyzer can typically find memory management problems even before you run your program.
  • The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction.
179 questions
112
votes
11 answers

When to use -retainCount?

I would like to know in what situation did you use -retainCount so far, and eventually the problems that can happen using it. Thanks.
Moszi
  • 3,208
  • 2
  • 20
  • 20
38
votes
2 answers

Calling -retainCount Considered Harmful

Or, Why I Didn't Use retainCount On My Summer Vacation This post is intended to solicit detailed write-ups about the whys and wherefores of that infamous method, retainCount, in order to consolidate the relevant information floating around SO.* The…
jscs
  • 62,161
  • 12
  • 145
  • 186
12
votes
2 answers

What is retainCount in Objective-C?

I have a UITableView as my first screen with a UINavigation controller. In my first screen I NSLog(@"Home Screen retain Count=%d",[self retainCount]); and it logs 6 in when its viewDidLoad is called. Is this correct?
Rahul Vyas
  • 26,593
  • 48
  • 175
  • 252
9
votes
2 answers

How to check the retain count while debugging

Does anybody know how can I check the retain count of an object while in debug mode? I have tried to add an expression [objInstance retainCount] but it did not work. I have also tried the print object PO [objInstance retainCount] in the console but…
mrd3650
  • 1,301
  • 3
  • 15
  • 23
8
votes
4 answers

What do you think about this code in Objective-C that iterates through retain count and call release every iteration?

I'm still trying to understand this piece of code that I found in a project I'm working on where the guy that created it left the company before I could ask. This is the code: -(void)releaseMySelf{ for (int i=myRetainCount; i>1; i--) { …
Ricardo
  • 2,438
  • 4
  • 26
  • 39
8
votes
4 answers

How to tell if object is in an NSAutoreleasePool

I would like to know how many times an object has been autoreleased. I've used objective c long enough that it's generally straight forward to know whether an object has been autoreleased or not, however I constantly see questions dealing with…
Sam
  • 25,752
  • 12
  • 68
  • 97
8
votes
3 answers

Overrelease issue with block-captured objects; retain count jumps straight from +2 to 0!

I'm confused by an occasional crash that I'm seeing, which, according to the Zombies instrument, is caused by the over-release of some dictionary values. When I look at the object history for one of these overreleased objects in Instruments, I see…
8
votes
0 answers

CFGetRetainCount returns 2 after creating an object, should be 1?

When I do a retain count on this object it is 2. Why is this? Surely it should be one as I have just initialised it and not assigned it or anything... let testC: TestClass = TestClass() print(CFGetRetainCount(testC)) This was done in an empty…
7
votes
2 answers

How many times do I release an allocated or retained object?

I am making an iPhone game. I want to release all objects that have been allocated or retained. In the dealloc function I am releasing all such objects, but then I realized that sometimes I end up releasing objects when they have not been allocated…
6
votes
3 answers

does addSubview increment retain count?

I've tested it and it looks like it does. So my question is, does it ALWAYS increment the retain count. So everytime I do something like this: UIView *theView = [[[UIView alloc] initWithFrame:(CGRect)aFrame] autorelease]; [self.view…
Thomas Clayson
  • 28,448
  • 25
  • 135
  • 216
6
votes
5 answers

Objective c - Reference counting

Until five minutes I was sure that my understanding about Objective c reference counting is excellent, but when I started checking objects retainCount I was very surprised to see what I saw. For example myViewController has a UITableview: .h …
Eyal
  • 10,429
  • 18
  • 73
  • 127
5
votes
1 answer

ARC: __bridge versus __bridge_retained using contextInfo test case

Consider this ARC code: - (void)main { NSString *s = [[NSString alloc] initWithString:@"s"]; [NSApp beginSheet:sheet modalForWindow:window modalDelegate:self …
5
votes
1 answer

How is retain count implemented in NSObject?

My question is how the current versions of Foundation (or of the Objective-C runtime library, since this seems to be there) implement retain count for NSObject derived objects? As I could see at NSObject.mm, there is no ivar called retain count in…
LuisABOL
  • 2,751
  • 3
  • 22
  • 53
4
votes
2 answers

retain count for mutable and immutable object in objective c?

NSArray *arr=[[NSArray alloc]init]; //Am getting all immutable objects allocation that retain count:2 NSLog(@"dic1:%d",[arr retainCount]); [arr retain]; [arr retain]; [arr retain]; [arr release]; NSLog(@"dic2:%d",[arr…
Snaking
  • 61
  • 7
4
votes
1 answer

Using Instruments not Showing Reference Count

I am struggling to determine what is holding on to objects that I am removing from the view hierarchy and setting to nil (and to the best of my knowledge setting all delegates to nil and removing all notifications) using Instruments Allocations…
Jeshua Lacock
  • 4,342
  • 1
  • 24
  • 48
1
2 3
11 12