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
0
votes
1 answer

How to identify who is referencing an object in objective C?

Is there any way to find, who is referencing an object in objective C? I have a scenario where I'm expecting an object to have retain count as zero but is is greater than zero. I have no clue at that point what other objects are referencing it. I…
Jitendra Singh
  • 2,103
  • 3
  • 17
  • 26
0
votes
2 answers

iPhone - Retain Count - Retain count goes up for no apparent reason

Quick question, hopefully I am just missing something simple. Ok I have one class that holds a pointer to another; MainMenuClass and NormalGameClass. Inside of the MainMenuClass I do the following. m_NormalGame = [[NormalGameMode alloc]…
Midnight
  • 5
  • 2
0
votes
1 answer

IBOutlet UIWebView retainCount

I have a View Controller in which I have UIwebView created in IB. IBOutlet UIWebView *webView; @property (nonatomic, retain) IBOutlet UIWebView *webView; @synthesize webView; this webView has retainCount = 2 in viewDidLoad. Why? Thanks
Burjua
  • 12,506
  • 27
  • 80
  • 111
0
votes
4 answers

Objective C: @property(retain) doesn't call retain?

I was trying to track a strage memory allocation bug so I overrode the retain and release methods of my class. I noticed that when assigning an instance of this class to a property of another, the retain count of the object increased, but my…
0
votes
1 answer

NSString and retainCount question

I have a NSString declared in the interface part: @property (nonatomic, retain) NSString *filePath; In viewDidLoad I give this a value and when I am trying to call it from one of my custom methods it works for the first time but on the second it…
CristiC
  • 22,068
  • 12
  • 57
  • 89
0
votes
2 answers

Retain Count of a View Controller

I am trying to print the retain count of a view controller in the viewDidLoad method. The value returned is 3 . Can someone please tell why is the value 3 ? My code is as follows @implementation ViewController - (void)viewDidLoad { …
Preetham Baliga
  • 271
  • 2
  • 4
  • 15
0
votes
1 answer

Why shouldn't I release when assigning to a retain property?

This question is related to this one, but simpler. [I think I may be close to the end of these dumb questions and can get down to serious business :)]. I have a retain property and set to it like this: UINavigationController *thing =…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
0
votes
3 answers

Retain object without using a property

I have a control that works like this: MyCustomControl *control = [[MyCustomControl alloc] initWithNavigationController:self.navigationController]; control.completion = ^{ [self controlCompletedAction]; }; [control…
ebi
  • 4,862
  • 6
  • 29
  • 40
0
votes
0 answers

How reference count work?

Suppose a test scenario Employee *aObj = [[Employee alloc]init]; NSLog(@"Retain count is1: %ld", CFGetRetainCount((__bridge CFTypeRef)aObj)); Employee *bObj = aObj; NSLog(@"Retain count is2: %ld", CFGetRetainCount((__bridgCFTypeRef)bObj)); Employee…
The iCoder
  • 1,414
  • 3
  • 19
  • 39
0
votes
1 answer

Objective-C++ and objects retaining

Consider this code example: class SomeArbitrarilyNamedClassPlusPlus { public: SomeArbitrarilyNamedClassPlusPlus(NSObject *object) { object_ = object; } SomeArbitrarilyNamedClassPlusPlus() { object_ = nil; } private: NSObject…
peetonn
  • 2,942
  • 4
  • 32
  • 49
0
votes
2 answers

Irregular retain counts of NSNumbers

I am using NSNumbers throughout my app (non-ARC) using different syntaxes. Just to be a little more informed, I tried to see how NSNumbers are retained depending on their initialization syntaxes. So I did the following: NSNumber* a = @1; NSNumber* b…
n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
0
votes
3 answers

make retain count in ARC

I am using an external library in my project which is being build in an ARC environment. As per the library the socket object gets deallocated only when the retain count=0. As far as I know its not liable to use retain count in ARC but I am forced…
user2955351
  • 273
  • 4
  • 18
0
votes
3 answers

Memory Management ios7

I have following code: - (IBAction)HeyCount:(UIButton *)sender { NSString* strr = [[NSString alloc] initWithString:@"hi there"]; self.string = @"789"; ohYeah = @"456"; NSLog(@"Retain Count of ohYeah:[%d] with String:[%ld]",[ohYeah…
Omer Obaid
  • 416
  • 1
  • 6
  • 24
0
votes
0 answers

NSArray retainCount

I thought it would be printed in order 0-1->0. This output was strangely different results. (0->3->2) Why is that? ClassA.h : @interface ClassA : NSObject { NSArray *array; } @property(nonatomic, retain)NSArray *array; @end main.c int…
realmasse
  • 523
  • 1
  • 6
  • 18
0
votes
3 answers

a base use of Objective-C's retainCount

We both know that the retain method will +1 retainCount, release will -1 retainCount, if retainCount == 0, the object dealloc. But I meet a problem, the following code run have the ridiculous result #import @interface…