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

  • Home
  • SEARCH
  • 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 956385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:31:52+00:00 2026-05-16T00:31:52+00:00

I finally understand whether I must release an object, and how to keep my

  • 0

I finally understand whether I must release an object, and how to keep my retain counts as low as possible in a trivial application (by knowing whether the object comes back with an extra ‘retain’ already called or not). So the NARC rules are working pretty well in that regard.

On the other hand, I can’t wrap my head around whether to retain (using retain properties, usually) or just assign instance variables. Are there any rules of thumb there? I know of only two:

  1. If I made the instance within my class (e.g., in an init), then I should retain it and release it in the dealloc.
  2. Delegates get assigned, not retained

But what I don’t understand is whether I should retain stuff that I have passed from another instance. I know that the risk is that the retain count goes down to zero and I am left holding the bag (or left holding many extra bags and memory), but… how should I organize my application so that this doesn’t happen?

I’ve seen some related questions, but nothing general.

While I’m Here: What about things that have been added to a UIWindow or UIView that I am not personally retaining? If their superview gets removed from all superviews, will the “naturally” retained instances get deallocated?

  • 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-05-16T00:31:53+00:00Added an answer on May 16, 2026 at 12:31 am

    how to keep my retain counts as low as possible

    I don’t understand this. What makes you think you need to worry about retain counts at all? The retain count is an implementation detail, forget about it. All you need to worry about is whether you own an object.

    I can’t wrap my head around whether to retain (using retain properties, usually) or just assign instance variables.

    In almost all cases you want to retain or copy when you assign instance variables. The main exception is retain cycles. Sometimes, you find yourself in a situation where object A has a reference to object B and object B has a reference to object A. If A retained B and B retained A, you cannot rely on the normal pattern of releasing references in dealloc to make them go away. In this situation, it is usual to nominate one object as the owner and the other as the owned. The owned object would then not retain its reference to the owner.

    An example of this would be a tree structure where the parent node has references to its children and each child has a reference to its parent. If each child retained its parent, when the application is done with the parent, it won’t go away, because it has been retained by all its children. So the parent reference is implemented as a weak reference which means that the child does not retain its parent.

    It’s a very common pattern with delegates for the owner of an object to set itself as the object’s delegate. For example, the controller of a table view is often also the table view’s data source and/or delegate. If the table view retained the delegate you would have a retain cycle (since the controller has already retained the view). This is why the convention “delegates aren’t retained” has arisen.

    You should definitely copy if the object you want a reference to implements NSCopying and you need it not to change while you have ownership. An example from Cocoa is the keys of elements in an NSDictionary. If the keys were to change while in the dictionary, it would be a disaster. So NSDictionary copies its keys.

    I know that the risk is that the retain count goes down to zero and I am left holding the bag (or left holding many extra bags and memory), but… how should I organize my application so that this doesn’t happen?

    Simple: follow the Cocoa memory management rules. If something goes away while you are holding a reference to it, you did not own that something. If you need something to hang around for longer than the scope of the current method, you should retain it.

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

Sidebar

Related Questions

I finally got xdebug set up and running and I'm using webgrind to understand
After reading Practical Common Lisp I finally understood what the big deal about macros
I finally got my group to switch from SourceSafe to Subversion. Unfortunately, my manager
I finally have a hardware guy that is insterested in controlling the firmware. This
I finally found out the difference between UTC and GMT by making the effort
I'm finally getting my team to embrace source code management now that we're working
I have finally finished my web site. I published it, and I was surprised
Since there is no finally in C++ you have to use the RAII design
Well the docs finally said it, I need to take it easy on my
I've finally completed a working version of my first ever CSS-supported site (thanks to

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.