Memory Release Problem

Discussion in 'iOS Programming' started by Appline, Apr 17, 2012.

  1. Appline macrumors newbie

    Joined:
    Apr 17, 2012
    #1
    My project got a scrollview with a large image (800x600 png) and animation.
    When a leave the view to another view my memory increase and i have a memory leak and crash.
    this is my code:

    S1001ipad.h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface S1001ipad : UIViewController<UIScrollViewDelegate>
    {UIImageView *imageview1;
    	UIImageView *imageview2;
    	UIImageView *imageview3;
    }
    
    
    @property (retain, nonatomic) IBOutlet UIButton *next;
    @end
    
    
    S1001ipad.m
    Code:
    
    
    #import "S1001ipad.h"
    
    @interface S1001ipad ()
    
    @end
    
    @implementation S1001ipad
    @synthesize next;
    
    UIImageView *animazione;
    const int numImages1001ipad = 3;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIScrollView* scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(112, 84, 800, 600)];
    	[scroll setDelegate:self];
        [scroll setShowsHorizontalScrollIndicator:NO];    
        [scroll setShowsVerticalScrollIndicator:NO];
    	scroll.contentSize = CGSizeMake(670 * numImages1001ipad, 741);
    	[scroll setContentSize:CGSizeMake(670 * numImages1001ipad, 741)];
    	scroll.contentOffset = CGPointMake(0, 0	);
    	scroll.pagingEnabled = TRUE;
    	scroll.contentSize = CGSizeMake(2410, 1);
    	imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 800, 600)];
    	[imageview1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"S101-00" ofType:@"png"]]];
    	[imageview1 setTag:1];
    	
    	imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(300, 0, 800, 600)];
    	[imageview2 setTag:2];
    	
    	imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(600, 0, 800, 600)];
    	[imageview3 setTag:3];
    	
    	[scroll addSubview:imageview1];
    	[scroll addSubview:imageview2];
    	[scroll addSubview:imageview3];
    	
    	
    	[self.view addSubview:scroll];
    	[scroll release];
        
    	// Do any additional setup after loading the view.    // Do any additional setup after loading the view.
        
    }
    
    
    
    -(void)scrollViewDidScroll:(UIScrollView*)scrollView
    {
        const CGFloat currPos = scrollView.contentOffset.x;
    	const NSInteger selectedPage = lroundf(currPos /700.0); 
    	const NSInteger zone = 1 + (selectedPage % 5); 
    	const NSInteger nextPage = selectedPage + 1;
    	const NSInteger prevPage = selectedPage - 1;
    	/// Next page
    	if (nextPage < numImages1001ipad)
    	{
    		[animazione stopAnimating];
            [animazione removeFromSuperview];
            
            NSInteger nextViewTag = zone + 1;
    		if (nextViewTag == 4)
    			nextViewTag = 1;
    		UIImageView* nextView = (UIImageView*)[scrollView viewWithTag:nextViewTag];
    		nextView.frame = CGRectMake(nextPage * 800, 0, 800, 600);
    		NSString *str = [NSString stringWithFormat:@"S101-0%d", nextPage];
    		UIImage* img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:str ofType:@"png"]];
    		nextView.image = img;
            NSLog(@"prepunteggio: %i ", nextViewTag );
           
    	}else {
            
        }
    	/// Prev page
    	if (prevPage >= 0)
    	{
    		NSInteger prevViewTag = zone - 1;
            [animazione stopAnimating];
            [animazione removeFromSuperview];
    		if (!prevViewTag)
    			prevViewTag = 4;
    		UIImageView* prevView = (UIImageView*)[scrollView viewWithTag:prevViewTag];
    		prevView.frame = (CGRect){.origin.x = prevPage * 800, .origin.y = 0.0f, .size = prevView.frame.size};
    		NSString *str = [NSString stringWithFormat:@"S101-0%d", prevPage];
    		UIImage* img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:str ofType:@"png"]];
    		prevView.image = img;
            next.enabled = NO;
            self.next.alpha = 0;
            
            
    	}
        if (zone == 3)
        {
            
            next.enabled = YES;
            self.next.alpha = 1;
            
            animazione = [[UIImageView alloc] initWithFrame:CGRectMake(112, 84, 800, 600)];
            // load all the frames of our animation
            NSMutableArray *a = [NSMutableArray array];
            //[a addObject: [UIViewController ]];
           
            [a addObject:[UIImage imageNamed:@"A101-02-00.png"]];
            [a addObject:[UIImage imageNamed:@"A101-02-01.png"]];
            [a addObject:[UIImage imageNamed:@"A101-02-00.png"]];
            [a addObject:[UIImage imageNamed:@"A101-02-02.png"]];
           
            animazione.animationImages = a;     
            // all frames will execute in 1.75 seconds
            animazione.animationDuration = 1
            ;
            // repeat the annimation forever
            animazione.animationRepeatCount = 0;
            // start animating
            [animazione startAnimating];
            // add the animation view to the main window 
            
            [self.view addSubview:animazione];
    [animazione setImage:nil];
        }
       
        
        
        
        
        
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    	return imageview1;
    }
    
    
    -(void)dealloc {
    	[imageview1 release];
    	[imageview2 release];
    	[imageview3 release];
        [animazione release]; 
        animazione.animationImages = nil;
        [animazione setImage:nil];
        [next release];
        
        [next release];
    	[super dealloc];
    }
    - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
        return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    }
    - (void)viewDidUnload {
       animazione.animationImages = nil;
        [self setNext:nil];
        [super viewDidUnload];
    }
    - (void)viewDidDisappear{
        
        [imageview1 release];
    	[imageview2 release];
    	[imageview3 release];
        [animazione release]; 
        animazione.animationImages = nil;
        [animazione setImage:nil];
    
    }
    @end
    
    I can't release anything.. Help me!!!
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    I see two things, but these may not be your problem, in dealloc you have [Next release]; twice.

    Also, In viewDidLoad I would release the imageViews after adding as subviews and not in the dealloc method.
     

Share This Page