{"id":3934,"date":"2014-03-30T06:26:19","date_gmt":"2014-03-30T06:26:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-retaincount-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:26:19","modified_gmt":"2014-03-30T06:26:19","slug":"problem-about-retaincount-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-retaincount-collection-of-common-programming-errors\/","title":{"rendered":"problem about retaincount-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/6c734598976438a1c24536a57709eded?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nOhad Regev<br \/>\nobjective-c memory-management automatic-ref-counting retaincount<br \/>\nThis question already has an answer here:How do I verify reference count in ARC mode?7 answersWhile using ARC, life is much easier in terms of memory management; but, let&#8217;s say I wish to look at a certain object while the app is running and see how many pointers are pointing to it at each certain point in the code. Is there a way to do that?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b0f1e0a3d326c9016a61fd34bcca0e05?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nLuisABOL<br \/>\nobjective-c cocoa nsobject objective-c-runtime retaincount<br \/>\nMy 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 the NSObject&#8217;s interface body. Instead, there seems to be a kind of table or map which contains references counters for each object. But if retain count is really done with a map, aren&#8217;t retain and release operations too expensive with this kind of imp<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/40db1e8b4e68aba25abafdb5885f9a8c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nlockedscope<br \/>\nobjective-c memory-management objective-c-blocks retain retaincount<br \/>\nWhen ARC enabled, following code causes the obj retained twice for the block by calling objc_retain() and objc_retainBlock().(So retainCount becomes 3 after the block definition.)I checked that obj is not in autorelease pool and there are two objc_release() calls for obj at the end of the method. (I know counting retains does not make sense but i am checking the objc_retain() calls, not barely checking retain counts.) What is the rationale behind this?-(void)myMethod {NSObject *obj = [[NSObject<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/Yt8Z9.jpg?s=32&amp;g=1\" \/><br \/>\nAndrey Chernukha<br \/>\nobjective-c memory-management copy retaincount<br \/>\nI know that my question has already been discussed on StackOverflow but i found the answer not complete for my needs. So the question is:NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil]; NSMutableArray *secondArray = [[NSMutableArray alloc] init]; secondArray = [firstArray mutableCopy];what is retain count for the secondArray now? 2 or 1? Should i release it twice or just once? Does copy or mutableCopy increases retain count of the COPYING (secondArray in this<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/79f438a6844f77869a79854eb9404aae?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJosh Caswell<br \/>\nobjective-c cocoa reference-counting retaincount<br \/>\nWhen 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&#8217;t the object be deallocated, and the retain count 0?NSMutableString *str=[[NSMutableString alloc] initWithString:@&#8221;hello&#8221;]; NSLog(@&#8221;reference count is %i&#8221;,[str retainCount]); [str release]; NSLog(@&#8221;reference count is %i&#8221;,[str retainCount]);I do see 0 for the retain count if I set str to nil first. Why is that?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/fb6c69436d239304e7c44db78ea2ea36?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nThomas Clayson<br \/>\niphone release retaincount<br \/>\nI&#8217;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 addSubview:theView];Am I actually leaking memory?I have a global property @property (nonatomic, retain) UILabel *ingredientsTextLabel; which I instantiate in viewDidLoad with this code:I just have the property named, theres no property for it in my header, so no getter<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/33978cde5a93656b7e3f8ee4faa50de4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNick Hutchinson<br \/>\nobjective-c instruments grand-central-dispatch retaincount nszombie<br \/>\nI&#8217;m confused by an occasional crash that I&#8217;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 that its retain count drops straight from +2 to 0 at one stage. (Take a look at the screenshots at the end of the post). It&#8217;s not clear to me how this is even possible. I should say that I only see this crash when profiling with Instruments, so I suppo<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/400306440d5ca693409f8abd37d7587a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nShanti K<br \/>\niphone modalviewcontroller dismiss retaincount deallocate<br \/>\nIn my app, I use method [self DismissModalView&#8230;] to dismiss a search view, everything was ok in iOS 3 and iOS 4, but now I upgraded to XCode 4.2 and SDK 5, this method runs ok againts iOS 5 but when I test it against iOS 3 + 4, the application crashes with message log:[CALayer retainCount]: message sent to deallocated instance 0xc640b80.I tried to investigate but still not find out root cause, please help me!<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9020a80cda52e7a425d4824ad572b241?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmttrb<br \/>\nios retaincount<br \/>\nNow I have a ClassA : NSObject, and then in the viewcontroller viewDidLoad, look at the under code:- (void)viewDidLoad {ClassA *a = [[ClassA alloc] init];NSLog(@&#8221;a retainCount = %d&#8221;, [a retainCount]);ClassA *b = a;NSLog(@&#8221;a retainCount = %d b retainCount= %d &#8220;, [a retainCount],[b retainCount]);[a release];NSLog(@&#8221;a retainCount = %d b retainCount= %d&#8221;, [a retainCount],[b retainCount]);[super viewDidLoad]; }The console output looks like:2012-11-02 14:43:35.437 RetainCountTest[1043:207] a retainCou<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5a19d0736fe857c359ed2a6ed2463107?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nnikans<br \/>\ncocoa memory-management release nil retaincount<br \/>\nHere I got some ugly code:NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@&#8221;yyyy&#8221;]; NSDate *date = [NSDate date]; NSString *textWithYear = [NSString stringWithFormat:@&#8221;text and year %@&#8221;, [dateFormatter stringFromDate:date] ]; [dateFormatter release]; NSLog(@&#8221;%i&#8221;, [dateFormatter retainCount]); \/\/ returns 1 !As you see, retains counter returns 1, which I suppose means that the object is not released. If I change that string to[dateFormatter release], d<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a127b68167d05cb1688cc7845c531ea1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPengOne<br \/>\nobjective-c ios uiapplicationdelegate addsubview retaincount<br \/>\nI was wondering something about the app delegate of my app. Why can&#8217;t I release like this :-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {RootViewController *controller = [[RootViewController alloc]initWithNibName:@&#8221;RootViewController&#8221;bundle:[NSBundle mainBundle]];[self.window addSubview:controller.view];[controller release]; \/\/ Here&#8217;s my question[self.window makeKeyAndVisible];return YES; }I was almost sure that -addSubview method inc<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/570ef06e4505624920dc069788b77b67?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRudiger<br \/>\niphone memory-management uiview release retaincount<br \/>\nI have an issue with releasing a view too many times. Although simple in theory because im moving uiview to uiview, which is a subclass of uiview and being animated and so on its not something that I can easily fix. It only crashes 10% and only under certain conditions and only 30% of the time even under these conditions.So in other words its kinda complex. Sometimes in my dealloc method the retain count of this UIView is already 1 (which gets released when the view is released) and so shouldn&#8217;<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f2b5fd2c449d2e8abad80c7d022de136?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMadMaxAPP<br \/>\niphone exc-bad-access retain retaincount<br \/>\nSerious Problem here&#8230; i&#8217;m getting ECX_BAD_ACCESS if i try to NSLog an instance variable of my custom object. Following Function is called in my ViewController, payload holds String Data which is pulled from a url.- (void) initVcardWithData:(NSString *)payload {NSLog(@&#8221;1. initVcardWithData&#8221;);aVCard = [[vcardItem alloc] initWithPayload:payload];VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];[self presentModalViewController:aVCardViewController anim<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9585031f188cb3459883c512f42a25f5?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNJones<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e6e0e2b73358e47e0b7f83f8111f75b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMattDiPasquale<br \/>\nobjective-c automatic-ref-counting retaincount unsafe-unretained<br \/>\nLet&#8217;s say I want to create a temporary variable, e.g.:To point to another long-living variable:__unsafe_unretained UIView *tableHeaderView = self.tableView.tableHeaderView;To point to an object I just created.__unsafe_unretained UIView *tableHeaderView = [[UIView alloc] init];These temporary variables don&#8217;t need to be retained because the objects they point to are guaranteed to keep positive retain counts for as long as the temporary variables are in scope. So, should I declare them as __unsafe_<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9dcb46c0895d6a55ac3f5f71ad8a5dc2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRon<br \/>\niphone memory cocos2d retain retaincount<br \/>\nI 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 yet. So I figured I need to check if its retainCount is greater than zero or not before I release it.My question is:Do I just check if the retainCount is greater than zero and then release it?if([bg retainCount]!=0) {[bg release]; }orShould I release<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a7cd283f886e60241d921289f3c83f23?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMartin<br \/>\niphone memory-management properties retaincount<br \/>\nMy question looks like a dozen ones about releasing properties, but I can&#8217;t find the exact answer i&#8217;m searching for.When a property is declared with retain :@property (nonatomic, retain) NSString * myString;then@synthesize myString;It generates getters and setters with retaining and releasing operations. Okay.Of course, property must be released in dealloc.-(void)dealloc {[myString release];myString = nil;[super dealloc]; }So that&#8217;s clean.But what if i never use myString ? Is it pre-initialised<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0bdae3fa4689bfeb77145c0aa94dbee5?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser2332733<br \/>\nobjective-c memory-management retaincount<br \/>\nHave a look at this code-snippet with a simple retain\/release scenario:#import &lt;Foundation\/Foundation.h&gt;@interface SomeClass : NSObject @end@implementation SomeClass @endint main(int argc, const char * argv[]) {SomeClass *aClass = [[SomeClass alloc] init];NSLog(@&#8221;retainCount: %lu&#8221;, [aClass retainCount]);[aClass retain];NSLog(@&#8221;retainCount: %lu&#8221;, [aClass retainCount]);[aClass release];NSLog(@&#8221;retainCount: %lu&#8221;, [aClass retainCount]);[aClass release];NSLog(@&#8221;retainCount: %lu&#8221;, [aClass retain<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ohad Regev objective-c memory-management automatic-ref-counting retaincount This question already has an answer here:How do I verify reference count in ARC mode?7 answersWhile using ARC, life is much easier in terms of memory management; but, let&#8217;s say I wish to look at a certain object while the app is running and see how many pointers are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3934","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3934"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3934\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}