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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:18:00+00:00 2026-05-19T04:18:00+00:00

I am newbie to iPhone programming. I am not using Interface Builder in my

  • 0

I am newbie to iPhone programming. I am not using Interface Builder in my programming. I have some doubt about memory management, @property topics in iPhone.
Consider the following code

@interface LoadFlag : UIViewController {
  UIImage *flag;
  UIImageView *preview;

}

@property (nonatomic, retain) UIImageView *preview;
@property (nonatomic, retain) UIImage *flag; 

...   

@implementation LoadFlag
@synthesize preview;
@synthesize flag;

- (void)viewDidLoad
{
  flag = [UIImage imageNamed:@"myImage.png"]];
  NSLog(@"Preview: %d\n",[preview retainCount]); //Count: 0 but shouldn't it be 1 as I am retaining it in @property in interface file
  preview=[[UIImageView alloc]init];
  NSLog(@"Count: %d\n",[preview retainCount]); //Count: 1 
  preview.frame=CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
  preview.image = flag; 
  [self.view addSubview:preview];
  NSLog(@"Count: %d\n",[preview retainCount]); //Count: 2
  [preview release];
  NSLog(@"Count: %d\n",[preview retainCount]); //Count: 1
}

...
  1. When & Why(what is the need) do I have to set @property with retain (in above case for UIImage & UIImageView) ? I saw this statement in many sample programs but didn’t understood the need of it.

  2. When I declare @property (nonatomic, retain) UIImageView *preview; statement the retain Count is 0. Why doesn’t it increase by 1 inspite of retaining it in @property.

  3. Also when I declare [self.view addSubview:preview]; then retain Count increments by 1 again. In this case does the “Autorelease pool” releases for us later or we have to take care of releasing it. I am not sure but I think that the Autorelease should handle it as we didn’t explicitly retained it so why should we worry of releasing it.

  4. Now, after the [preview release]; statement my count is 1. Now I don’t need UIImageView anymore in my program so when and where should I release it so that the count becomes 0 and the memory gets deallocated. Again, I am not sure but I think that the Autorelease should handle it as we didn’t explicitly retained it so why should we worry of releasing it. What will happen if I release it in -(void) dealloc method

  5. In the statement -> flag = [UIImage imageNamed:@"myImage.png"]]; I haven’t allocated any memory to flag but how can I still use it in my program. In this case if I do not allocate memory then who allocates & deallocates memory to it or is the “flag” just a reference pointing to -> [UIImage imageNamed:@"myImage.png"]];. If it is a reference only then do i need to release it.

  • 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-19T04:18:00+00:00Added an answer on May 19, 2026 at 4:18 am

    Question 1

    This means that the synthesized property accessor messages will include an automatic retain when the message is called (but ONLY when the message is called, see next).

    Question 2

    This is because you are not using the property accessor message, you are just assigning to the member variable. If you use:

    self.preview = [[[UIImageView alloc] init] autorelease];
    

    The resulting retain count will be one (+1 for the init, -1 for the autorelease, +1 for the retain on the message).

    N.B.

    You will get the same retain count (one) if you do this:

    preview = [[UIImageView alloc] init];
    

    (+1 for the init, not using the property accessor message so no extra retain). Up to you which way you go with.

    Question 3

    The addSubview will increment the retain count again because the preview will be stored in a collection which will retain it’s objects.

    So yes, Basically if you are handing an object off to another object to manage (as is the case with addSubview) you can set it to autorelease and it will be released by the other object. However, since you are storing the UIImageVIew in a retained property, you will need to release it yourself (see next).

    Question 4

    Because you are keeping the preview object as retained property, you will need to release it in your dealloc message. So in my Question 2 example, you allocate the object, autorelease it, but assign it to retained property, so the retain count after all that will be one, you are adding it to a collection which will also retain it. When the view is cleaned up the collection will decrement the retain count, but you will need to call release as well, because you stored it in a retained property. So in your dealloc:

    [preview release];
    

    Question 5

    imageNamed is a helper message that does the allocation, initialization and autorelease. So basically it is equivalent to saying.

    NSData * dataForImage = get data from the myImage.png here ...
    self.flag = [[[UIImage alloc] initWithData:dataForImage] autorelease];
    

    You are storing it in a retained property (because I use self.flag in the above example), so you will need to release it in the dealloc message.

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

Sidebar

Related Questions

I am a newbie for iPhone Programming. I would like to know how can
newbie programmer and lurker here, hoping for some sensible advice. :) Using a combination
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Java Newbie here. I have a JFrame that I added to my netbeans project,
I'm a newbie to pgsql. I have few questionss on it: 1) I know
Sorry for the second newbie question, I'm a developer not a sysadmin so this
I have never developed a game before and actually never developed anything for iPhone.
Newbie to LINQ, and trying to write the following query... select f.Section_ID, f.Page_ID, f.SortOrder,
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
.NET newbie here... I'd like to make a button in a Windows form that

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.