Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:58:05+00:00 2026-06-01T10:58:05+00:00

RetainCount == BAD retainCount is taboo, unreliable, unpredictable, and in general shouldn’t be used.

  • 0

RetainCount == BAD

retainCount is taboo, unreliable, unpredictable, and in general shouldn’t be used. I don’t use it anywhere in my code, but I have seen it in one class that I use in an interesting way.

I have a class that runs a thread that runs indefinitely until the thread is cancelled. The catch is that the thread increases the retain count of the owner, in my case the class that instantiated it. So, even if I am done using that class, that instance is still going to hang around unless whoever is managing my class also has the smarts to know to shut down the thread. That is one solution, but this is what I found in code.

- (oneway void)release
{
    // This override allows allows this object to be dealloced 
    // by shutting down the thread when the thread holds the last reference.
    // Otherwise, the object will never be dealloc'd
    if (self.retainCount == 2)
    {
        [self quitDispatchThread];
    }

    [super release];
}

This is a clever solution, but I’m not sure what to think of it. It overrides release on the class and checks to see if the retain count is 2. In other words, it checks to see if the thread is the only thing keeping my object alive (since the retain count is about to be decremented from 2 to 1) and if it is, it terminates the thread (quitDispatchThread will block until the thread is terminated).

So…

Can you rely on retainCount to see if it is one?

Usually people say to stay clear of retainCount because you don’t know if there are some autoreleases in there. However, if the retainCount is one then I know for a fact that only the thread is keeping it alive and I don’t have to be concerned that the retainCount might be off due to some autoreleases, etc…

What’s wrong with this code?

I was about to remove it, but it actually seems to make sense. Other objects don’t have to have an awareness that my class is running a thread. Other objects can safely retain and release or even autorelease the object owning the thread without having to worry about shutting the thread down because it takes care of itself.

This code actually feels clean, which surprises me.

Edit :: NSThread is Retaining my Object

The retain count of my object is increased by the fact I’m using NSThread. My object is the target and the selector is the method the thread runs on.

initWithTarget:selector:object:

Returns an NSThread object initialized with the given arguments.

  • (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument

Parameters

target

The object to which the message specified by selector is sent.

selector

The selector for the message to send to target. This selector must
take only one argument and must not have a return value.

argument

The single argument passed to the target. May be nil.

Return Value

An NSThread object initialized with the given arguments.

Discussion

For non garbage-collected applications, the method selector is
responsible for setting up an autorelease pool for the newly detached
thread and freeing that pool before it exits. Garbage-collected
applications do not need to create an autorelease pool.

The objects target and argument are retained during the execution of the detached thread. They are released when the thread finally
exits.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:58:06+00:00Added an answer on June 1, 2026 at 10:58 am

    retainCount is taboo, unreliable, unpredictable, and in general shouldn’t be used.

    You can rely on the value of retainCount IFF your objects don’t pass through any code that’s opaque to you, such as any Cocoa framework. In practice, this is almost impossible to achieve, thus the warning. The internals of Cocoa may be passing your object around, retaining, releasing, and putting it into autorelease pools many times and for many reasons, and you can’t rely on its absolute value at any given point.

    The catch is that the thread increases the retain count of the owner, in my case the class that instantiated it.

    That’s a retain cycle. The answer here is to find a way to break that cycle, not to subvert the reference counting mechanism. There must be some point before deallocation when either your thread or the owning object knows that the work the thread is performing is done (or needs to stop prematurely).

    It sounds like the owning object is the interface for client code to the work the thread is doing. This owning object needs a “shutdown now” method that needs to be called (and documented as “must be called”) before its owner releases it. In that shutdown method, you can break the cycle by releasing the thread.

    I’m not exactly sure what’s going on that the thread is retaining its creator (the cycle is a pretty clear indication that something is wrong with your ownership model) — I’d guess you’re using NSThread and initWithTarget:..., with the target being the creating/owning object. This is a bit of a mixup of the standard MVC pattern — the owner of the thread is a “controller”, the thread itself (and the code it runs) more of a “model”.

    The controller should not contain the thread’s code, in other words. I’d recommend that you factor out the thread’s code into another object to use as the target. Then the controller object owns both the thread itself and the “work” target object, with neither of them owning the controller. Voilà, no cycle!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help, i have some code and i am checking the retainCount but
I have used [anArray retainCount] to get the retain count of array..(i know this
Possible Duplicate: NSNumber retain count issue Hello, I have the following code: NSNumber *number
I have been working with Objective-C for a month approximately but regretfully I'm still
I have been programming on iPhone for quite sometime and have had bad experiences
I have a very useful macro defined in .gdbinit define rc call (int)[$arg0 retainCount]
I have an instance of AVPlayer in my application. I use the time boundary
NOTE: THE PROBLEM IS NOT IN THIS CODE, BUT THE ANSWER FOR EXC_BAD_ACCESS is
Short Question with a code example: NSLog(@%i, [[[NSArray alloc] init] retainCount]); NSLog(@%i, [[[NSMutableArray alloc]
Here is retaincount code. #import <Foundation/Foundation.h> int main (int argc, const char * argv[])

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.