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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:56:55+00:00 2026-05-22T01:56:55+00:00

I got a UIImageView that i set in the nib file. I download an

  • 0

I got a UIImageView that i set in the nib file. I download an image from internet and sets the image to the UIImageView. When i’m releasing it has retain count 2? If i’m using only 1 release it won’t show any memory leak but i can see in “Instrument Allocations” that it never gets released. When i release the UIImageView twice like below then it works good. But i should never release it twice?!?!

in Header:

IBOutlet UIImageView *background;

in the .m loading the image:

 /* Load Image code */
 id path = [NSString stringWithFormat:@"http://www.image.com/aImage.jpg"];
 NSURL *url = [NSURL URLWithString:path];
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
 [background setImage:[UIImage imageWithData:urlData]];
 [urlData release];
 [pool release];

in dealloc function:

- (void)dealloc {
    NSLog(@"Backgroud count: %i",[background retainCount]); // Prints 2
    [background release];
    [background release]; // Does not "leak" if i have 2x release
    [super dealloc];
}

This is the only code that is useing the UIImageView Background.

EDIT:

something i forgot to mention is that i run this code inside a for loop like this. but this for loop will only execute once! But it shouldn’t matter?

for (id theKey in dictionary) {
     /* Load Image code above is here */
}
  • 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-22T01:56:55+00:00Added an answer on May 22, 2026 at 1:56 am

    I believe that I’ve figured out what the trouble is. Apple recommends that you retain the objects you connect to through IBOutlets (your image view, in this case). You said that you haven’t done so, but you should be following Apple’s recommendation. The reason that you should is outlined in a iphonedevsdk.com forum post about this problem, which links to a Big Nerd Ranch blog post that lays it all out.

    On iOS, the nib loading mechanism uses the setter if your outlet has one, but it uses key-value coding if not; specifically, it uses setValue:forKey:, which retains the value (this is documented, but somewhat unexpected). Your image view, being the subview of your view controller’s top view, is retained by that view. It’s also retained by this key-value setting procedure. So, unbeknownst to you, your objects have two references to the image view. Apple makes the retaining property suggestion so that it becomes knownst to you that the view is being retained.

    You still shouldn’t be worrying about the retain count as such, but you should do one of two things: make this IBOutlet a retained property and release it in both viewDidUnload and dealloc (just once each, though!), or follow BNR’s suggestion and make the property explicitly assigned:

    @property (assign, nonatomic) IBOutlet UIImageView *background;
    

    in which case you do not have to release it yourself. In both cases, make sure you @synthesize the property accessors.


    Previously:

    Don’t look at retain count, and if there’s no leak being detected, then don’t worry about it. The UIKit framework is likely retaining the view for reasons that you aren’t privy to.

    Additionally, if background isn’t a retained property:

    @property (retain) IBOutlet UIImageView *background;
    

    and you’re creating it in the xib, you shouldn’t be releasing it at all, because you don’t own it. That is, you aren’t responsible for its memory; the actions that give you that responsibility are: calling retain on the object, or creating it using a method whose name begins with alloc, copy, mutableCopy, or new.

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

Sidebar

Related Questions

i've got an UIImageView, now i want to repeat this image, that it always
Got a seg fault from my memcpy that gdb can't give me anything else
got some problems. Built an applet that has to be used step-by-step. After each
Gentleones, I've got a UIImageView that I have in a UIView. This particular UIImageView
I've got an UIImageView as a background image for my application, which, like a
I have got an UIScrollView that contain UIImageView. I need to zoom my UIImageView.
UIImageView not show/display Original Image as that of available on server. for more ref
Ok, I've got a UITableView with custom UITableViewCell s that each contain a UIImageView
I got a UIView that contains an UIIMageView and several buttons that are allocated
I've got an UIImageView on top of (and not an element of) a UIScrollView

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.