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 7178081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:50:52+00:00 2026-05-28T16:50:52+00:00

I have read that dealloc for an object will be called, only if retain

  • 0

I have read that dealloc for an object will be called, only if retain count of that object becomes zero.

I am taking one object for UIColor in interface section and setting property

UIColor *currentColor;
@property (nonatomic, retain) UIColor *currentColor;

After using this object in the implemetation section, I am calling release method for this object in dealloc

-(void)dealloc
{
    [currentColor release];
    [super dealloc];
}

I am in doubt how dealloc will be called for this object, because I am not releasing the retained object anywhere. Thanks in advance.

  • 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-28T16:50:53+00:00Added an answer on May 28, 2026 at 4:50 pm

    I have read that dealloc for an object will be called, only if retain
    count of that object becomes zero.

    Yes.

    For the sake of simplicity, call the class that contains currentColor object as ColorContainer. Now, if you create an instance of ColorContainer like the following:

    ColorContainer* containerColor = [[ColorContainer alloc] init]; // retain count + 1
    

    the retain count for containerColor becomes 1.

    Suppose you create an instance of a UIColor and you set that instance to currentColor property. In this case you can follow two different ways.
    In the first one you can create an instance like the following. If you use an instance method like initWithRed:green:blue:alpha: you have to release memory explicitly.

    UIColor color* = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]; // retain count + 1
    containerColor.currentColor = color; // retain count +1, the referenced object has a retain count of 2 because you use a retain policy
    [color release]; // retain count -1, now  the referenced object has a retain count of 1
    

    In the second way, instead, you could use a class method (+ symbol). In this case you don’t need to release memory explicity because the object created in that class method will be autoreleased at a certain point of your application lifetime.

    containerColor.currentColor = [UIColor whiteColor]; // retain count +1
    

    Now suppose you release containerColor object. If the retain count for containerColor is equal to 1, releasing it from an object that uses it, it enables to call its dealloc method and, in consequence, to dismiss also the object referenced by currentColor.

    In this simple case study you have to note that the object referenced by currentColor is completely removed from memory (dismissed) only if it has a retain count of 1. In fact, if you do this

    UIColor color* = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]; // retain count + 1
    containerColor.currentColor = color; // retain count +1, the referenced object has a retain count of 2
    //[color release];
    

    you create a memory leak (Do you understand way?).

    To summarize, when you use retain, copy, init or new (it’s the same of alloc-init), you have always to call their counterparts release or autorelease.

    As a rule of thumb, you need always to balance the retain count for objects to avoid memory leaks.

    So, as a methaphor you could think to memory like a tree. Suppose you have a parent (containerColor) and a child (currentColor). If the parent (with a retain of count of 1) is released, it causes to call its dealloc method and free memory for its object. If in its dealloc method you release a child (with a retain count of 1) it causes to call its dealloc method and free memory. In the case a child has a retain count greater than one, you cause a memory leak.

    Hope it helps.

    Edit

    For further information you could read About Memory Management. Since iOS 5 Apple has introduced ARC. Automatic Reference Counting is a compiler machanism that provides automatic memory management of Objective-C objects. For info see Transitioning to ARC Release Notes.

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

Sidebar

Related Questions

I have read that calloc (malloc+init) will sometimes fail to init array with zero
I have read that one could use the class dump command line tool to
I have read that recursive implementation of QuickSort will require O(log n) of additional
I have read that passing -c when running rails generate will add the new
I have read that the following code snippet will result in a compiler error
I have read that errors.add_to_base should be used for errors associated with the object
I have read that PHPs probable weakness is how it handles concurrency. With only
I have read that R uses only a single CPU. How can I let
I have read that PHP is obsoleting the MySql functions. How will we connect
I have read that using command pattern is one of the most popular ways

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.