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.
180 questions
2
votes
1 answer

How does retain count with synchronous dispatch work?

I'm trying to explain ownership of objects and how GCD does its work. These are the things I've learned: a function will increase the retain count of the object its calling against a dispatch block, unless it captures self weakly will increase the…
2
votes
3 answers

Will the retain count increase when added to an array?

I just wanted to know: will the retain count of an object be incremented if it is added to an array or dictionary in Objective-C? Can I release a particular object immediately after adding it to an array or dictionary?
Hariprasad
  • 1,094
  • 2
  • 12
  • 30
2
votes
2 answers

Reference count is still 1 after [obj release], when it should be deallocated

When I create an object and check its retain count, I get 1 as expected. When I release the object and then check the retain count again, it is still 1. Shouldn't the object be deallocated, and the retain count 0? NSMutableString…
devaditya
  • 321
  • 9
  • 21
2
votes
4 answers

Weird behaviour with retainCount

When I am logging retain count with NSArray and NSString objects, I am having uneven behavior. See the code below, NSArray *aryTemp = [NSArray arrayWithObjects:@"One",nil]; NSLog(@"Retain Count :%d",[aryTemp retainCount]); NSArray *aryTemp2 =…
Pratik Goswami
  • 334
  • 1
  • 4
  • 15
2
votes
3 answers

This Swift code should produce a memory leak but it doesn't. Can someone point out why?

I think I know what't a retain cycle is on Swift and why it produces a memory leak. But I wrote a small example to demonstrate it and seems the code is correctly deallocated anyway. In this example I have two objects that retain each other (creating…
IgnazioC
  • 4,554
  • 4
  • 33
  • 46
2
votes
3 answers

why is the retain count showing a value 2 in the below code?

NSLog(@"retain count 1 for show detail -- %d",[showDetail retainCount]); ChecklistDetail *detail = [appDelegates.arrayForChecklistDetails objectAtIndex:[sender tag]]; self.showDetail = detail; NSLog(@"retain count 2 for show detail --…
mayuur
  • 4,736
  • 4
  • 30
  • 65
2
votes
6 answers

Objects' retain counts never go below 1 despite deliberately overreleasing

I am checking on the retain count of some objects NSLog(@"r = %d", [aObject retainCount]; It seems that the lowest value I can get is "r = 1", even if I deliberately add extra "release" calls [aObject release]; The "r = 1" limit holds even if I…
Stanley
  • 4,446
  • 7
  • 30
  • 48
2
votes
3 answers

Conditions in which retainCount increases or decreases

I have read that objects retain count and that it can be increased when we assigned a second value (or object). Can anybody give me an idea about the basic conditions where retainCount increases or decreases (without retain , alloc and release)...
Ranjeet Sajwan
  • 1,925
  • 2
  • 28
  • 60
2
votes
1 answer

NSMutableArray remove object increases reference count?

I have some code that is causing memory leaks on an iOS static library. Here is an object's lifetime from Instruments: # Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller 0 Table_ColumnListener Malloc…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
2
votes
1 answer

Using 'self' on RxSwift closures... What about instance methods as param?

In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the…
dsapalo
  • 1,819
  • 2
  • 19
  • 37
2
votes
5 answers

Obj-C Memory Management Setter Method

I am new to objective-c and I've downloaded the code from here. Ran Chapter 10, 10.01 CarPartsInit xcode project file. One thing I am not clear about is that, does the memory management of a setter method - (void) setEngine: (Engine *) newEngine { …
user372705
  • 81
  • 4
2
votes
5 answers

Why does sending retainCount to @"Hi" return -1?

The method retainCount is supposed to return an unsigned integer. Why then, does [@"Hi" retainCount] return -1?
SVA
  • 891
  • 1
  • 6
  • 6
2
votes
3 answers

Class retain counts

Plenty is posted here about avoiding retain cycles with blocks, but what about when using classes and class methods? Say I have a class like this: // MyClass.h + (void)doSomethingAsynch:(void (^)(void))block; + (void)doSomethingElse; and callers…
danh
  • 62,181
  • 10
  • 95
  • 136
2
votes
2 answers

Why is the retain count of @1 equal to 7, 8 or 10?

I created an empty iOS app on Xcode 4.4.1, and did the following: NSNumber *n1 = @1; NSNumber *n2 = @2; NSNumber *n3 = @3; NSNumber *n100 = @100; NSString *s = @"haha"; NSArray *a = @[n1, s]; NSDictionary *d = @{ @"ha" : @1, @3 : @"hello"…
Jeremy L
  • 3,770
  • 6
  • 41
  • 62
2
votes
3 answers

Retain count behavior for NSOperation

Does inserting an NSOperation to a NSOperationQueue increments the retain count of the NSOperation? If YES when will it get decremented?
1 2
3
11 12