0
votes

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. :-P

Cheers, Josiah

3

3 Answers

1
votes

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
votes

Ideally, class level variables should be released in the class' dealloc() method

Looks like imgView is a class level variable and you are releasing that at the end of the method call.

If you have a variable that you would like to release at the end of the method -- it should be a method-level variable. So, in your case, try making the imgView a method level variable. Then you should be able to release it at the end of the method without any problem

0
votes

Why do you create a temporary view? Do you call loadView more than once?

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[self.imageView setContentMode:UIViewContentModeCenter];
[self.imageView setImage:hiResImage];
[mainScrollView addSubview:self.imageView];
[self.imageView release]

Or release it in dealloc since it's an instance variable? addSubview retains your UIImageView, so it would be weird if it crashed because the object isn't present. Have you tried setting out breakpoints?