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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:09:45+00:00 2026-05-14T01:09:45+00:00

I’m having a memory problem related to UIImageView. After adding this view to my

  • 0

I’m having a memory problem related to UIImageView. After adding this view to my UIScrollView, if I try to release the UIImageView the application crashes. According to the stack trace, something is calling [UIImageView stopAnimating] after [UIImageView dealloc] is called. However, if I don’t release the view the memory is never freed up, and I’ve confirmed that there remains an extra retain call on the view after deallocating…which causes my total allocations to climb quickly and eventually crash the app after loading the view multiple times. I’m not sure what I’m doing wrong here though…I don’t know what is trying to access the UIImageView after it has been released. I’ve included the relevant header and implementation code below (I’m using the Three20 framework, if that has anything to do with it…also, AppScrollView is just a UIScrollView that forwards the touchesEnded event to the next responder):

Header:

@interface PhotoHiResPreviewController : TTViewController <UIScrollViewDelegate> {

    NSString* imageURL;
    UIImage* hiResImage;
    UIImageView* imageView;
    UIView* mainView;
    AppScrollView* mainScrollView;
}

@property (nonatomic, retain) NSString* imageURL;
@property (nonatomic, retain) NSString* imageShortURL;
@property (nonatomic, retain) UIImage* hiResImage;
@property (nonatomic, retain) UIImageView* imageView;

- (id)initWithImageURL:(NSString*)imageTTURL;

Implementation:

@implementation PhotoHiResPreviewController

@synthesize imageURL, hiResImage, imageView;


- (id)initWithImageURL:(NSString*)imageTTURL {

    if (self = [super init]) {

        hiResImage = nil;

        NSString *documentsDirectory = [NSString stringWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
        [self setImageURL:[NSString stringWithFormat:@"%@/%@.jpg", documentsDirectory, imageTTURL]];
    }
    return self;
}

- (void)loadView {

    [super loadView];

    // Initialize the scroll view   
    hiResImage = [UIImage imageWithContentsOfFile:self.imageURL];
    CGSize photoSize = [hiResImage size];
    mainScrollView = [[AppScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mainScrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    mainScrollView.backgroundColor = [UIColor blackColor];
    mainScrollView.contentSize = photoSize;
    mainScrollView.contentMode = UIViewContentModeScaleAspectFit;
    mainScrollView.delegate = self;

    // Create the image view and add it to the scrollview.
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
    tempImageView.contentMode = UIViewContentModeCenter;
    [tempImageView setImage:hiResImage];
    self.imageView = tempImageView;
    [tempImageView release];    
    [mainScrollView addSubview:imageView];

    // Configure zooming.
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    CGFloat widthRatio = screenSize.width / photoSize.width;
    CGFloat heightRatio = screenSize.height / photoSize.height;
    CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
    mainScrollView.maximumZoomScale = 3.0;
    mainScrollView.minimumZoomScale = initialZoom;
    mainScrollView.zoomScale = initialZoom;
    mainScrollView.bouncesZoom = YES;

    mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mainView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    mainView.backgroundColor = [UIColor blackColor];
    mainView.contentMode = UIViewContentModeScaleAspectFit;
    [mainView addSubview:mainScrollView];   

    // Add to view  
    self.view = mainView;
    [imageView release];
    [mainScrollView release];   
    [mainView release];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

- (void)dealloc {

    mainScrollView.delegate = nil;
    TT_RELEASE_SAFELY(imageURL);
    TT_RELEASE_SAFELY(hiResImage);
    [super dealloc];
}

I’m not sure how to get around this. If I remove the call to [imageView release] at the end of the loadView method everything works fine…but I have massive allocations that quickly climb to a breaking point. If I DO release it, however, there’s that [UIImageView stopAnimating] call that crashes the application after the view is deallocated.

Thanks for any help! I’ve been banging my head against this one for days. 😛

Cheers,
Josiah

  • 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-14T01:09:46+00:00Added an answer on May 14, 2026 at 1:09 am

    Move the release of imageView before the point where you set it. This way you release the object before changing where the imageView pointer points to:

    // Create the image view and add it to the scrollview.
    UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
    tempImageView.contentMode = UIViewContentModeCenter;
    [tempImageView setImage:hiResImage];
    // release old object
    [imageView release];
    // set the pointer to point at a new object
    self.imageView = tempImageView;
    [tempImageView release];    
    [mainScrollView addSubview:imageView];
    

    Then at the bottom of your method where you release the other objects, remove the line:

    [imageView release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In WQL, there isn't a JOIN operator. You will need… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer This question is too vague: the terms "range", "cluster", and… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer Not sure if there is a more efficient way, but… May 14, 2026 at 9:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.