Help With Releasing Data?

Discussion in 'iOS Development' started by SpeedyApocalypse, May 29, 2010.

  1. SpeedyApocalypse

    SpeedyApocalypse Member

    Joined:
    Dec 27, 2009
    Messages:
    564
    Likes Received:
    13
    Device:
    4G iPod touch
    EDIT: I found out that pretty much all of the leaks have to do with the AVAudioPlayer re-initializing itself every time. How would I have it always initialized so I don't have to do it on every view?

    I have an application in development, and after a few uses and view switches, the object that is moving starts to get really slow. Every time that I switch to a new view, it adds 0.10mb of data that does not get released when the view is moved away. I have tried releasing stuff for hours and I cannot figure it out. I was hoping that someone could show me where I have gone wrong in my code.

    Here is the .h file:

    [OBJC]#import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    #import "Level2.h"
    #import "RootViewController.h"


    @interface Play : UIViewController <AVAudioPlayerDelegate> {
    IBOutlet UIImageView *player;
    IBOutlet UIImageView *wall;
    IBOutlet UIImageView *wall1;
    IBOutlet UIImageView *wall2;
    IBOutlet UIImageView *wall3;
    IBOutlet UIImageView *wall4;
    IBOutlet UIImageView *wall5;
    IBOutlet UIImageView *wall6;
    IBOutlet UIImageView *wall7;
    IBOutlet UIImageView *nextLevel;
    UIImageView *scare;
    NSTimer *moveDownTimer;
    NSTimer *moveLeftTimer;
    NSTimer *moveRightTimer;
    NSTimer *moveUpTimer;
    NSTimer *collision;
    NSTimer *scaryFace;
    NSTimer *showButton;
    AVAudioPlayer *playScream;
    AVAudioPlayer *Audioplayer;
    IBOutlet UIButton *faceDismissButton;
    IBOutlet UILabel *dismissFaceLabel;
    }

    @property (nonatomic, retain) AVAudioPlayer *Audioplayer;

    -(IBAction)down;
    -(IBAction)downStop;
    -(IBAction)left;
    -(IBAction)leftStop;
    -(IBAction)right;
    -(IBAction)rightStop;
    -(IBAction)up;
    -(IBAction)upStop;
    -(IBAction)home;
    -(IBAction)dismissFace;

    @end[/OBJC]

    Here is the .m file:

    [OBJC]#import "Play.h"


    @implementation Play

    @synthesize Audioplayer;

    -(void)viewDidDisappear

    Please Register or Log in to view images

    BOOL)animated {
    [wall release];
    [wall1 release];
    [wall2 release];
    [wall3 release];
    [wall4 release];
    [wall5 release];
    [wall6 release];
    [wall7 release];
    [nextLevel release];
    [player release];
    [faceDismissButton release];
    [dismissFaceLabel release];
    [playScream release];
    [scare release];
    [AVAudioPlayer release];
    }

    -(void)viewDidLoad {

    faceDismissButton.hidden = YES;
    dismissFaceLabel.alpha = 0.0;

    NSString *path1 = [[NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "Scream" ofType

    Please Register or Log in to view images

    "wav"];
    NSURL *scream = [[NSURL alloc] initFileURLWithPath

    Please Register or Log in to view images

    ath1];

    playScream = [[AVAudioPlayer alloc] initWithContentsOfURL:scream error:nil];

    [scream release];

    self.Audioplayer = playScream;

    [Audioplayer prepareToPlay];
    [Audioplayer setDelegate:self];

    int faceTime = arc4random() % 15 + 15;
    collision = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector

    Please Register or Log in to view images

    selector(collision) userInfo:nil repeats:YES];
    scaryFace = [NSTimer scheduledTimerWithTimeInterval:faceTime target:self selector

    Please Register or Log in to view images

    selector(scaryFace) userInfo:nil repeats:NO];

    [super viewDidLoad];
    }

    -(void)scaryFace {

    showButton = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector

    Please Register or Log in to view images

    selector(showDismissButton) userInfo:nil repeats:NO];

    int x = arc4random() % 6 + 1;
    NSString *imageName = [[NSString alloc] initWithFormat

    Please Register or Log in to view images

    "face%i.png", x];

    CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
    scare = [[UIImageView alloc] initWithFrame:myImageRect];
    scare.opaque = YES;
    [self.view addSubview:scare];

    [playScream play];

    scare.image = [UIImage imageNamed:imageName];

    [playScream release];
    [scare release];
    [AVAudioPlayer release];
    [imageName release];
    }

    -(void)showDismissButton {
    faceDismissButton.hidden = NO;
    [self.view bringSubviewToFront:dismissFaceLabel];
    dismissFaceLabel.alpha = 1.0;
    [showButton invalidate];
    }

    -(IBAction)dismissFace {
    UINavigationController *navController = self.navigationController;
    RootViewController *home = [[RootViewController alloc] initWithNibName

    Please Register or Log in to view images

    "RootViewController" bundle:nil];
    [navController pushViewController:home animated:YES];
    [home release];
    }

    -(void)collision {

    if (CGRectIntersectsRect (player.frame, wall.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    }

    if (CGRectIntersectsRect (player.frame, wall1.frame)) {
    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, wall2.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, wall3.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, wall4.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, wall5.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, wall6.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }

    if (CGRectIntersectsRect (player.frame, nextLevel.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Level2 *level2 = [[Level2 alloc] initWithNibName

    Please Register or Log in to view images

    "Level2" bundle:nil];
    [navController pushViewController:level2 animated:YES];
    [level2 release];
    }

    if (CGRectIntersectsRect (player.frame, wall7.frame)) {

    [scaryFace invalidate];
    [collision invalidate];
    UINavigationController *navController = self.navigationController;
    Play *play = [[Play alloc] initWithNibName

    Please Register or Log in to view images

    "Play" bundle:nil];
    [navController pushViewController

    Please Register or Log in to view images

    lay animated:NO];
    [play release];
    }
    }

    -(IBAction)down {
    moveDownTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector

    Please Register or Log in to view images

    selector(moveDown) userInfo:nil repeats:YES];
    }

    -(void)moveDown {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:0.01];
    CGPoint center = (CGPoint) [player center];
    center.y +=1.0f;
    [player setCenter:center];
    [UIView commitAnimations];
    }

    -(IBAction)downStop {
    [moveDownTimer invalidate];
    [moveDownTimer release];
    }

    -(IBAction)left {
    moveLeftTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector

    Please Register or Log in to view images

    selector(moveLeft) userInfo:nil repeats:YES];

    }

    -(void)moveLeft {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:0.01];
    CGPoint center = (CGPoint) [player center];
    center.x -=1.0f;
    [player setCenter:center];
    [UIView commitAnimations];
    }

    -(IBAction)leftStop {
    [moveLeftTimer invalidate];
    [moveLeftTimer release];
    }

    -(IBAction)right {
    moveRightTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector

    Please Register or Log in to view images

    selector(moveRight) userInfo:nil repeats:YES];

    }

    -(void)moveRight {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:0.01];
    CGPoint center = (CGPoint) [player center];
    center.x +=1.0f;
    [player setCenter:center];
    [UIView commitAnimations];
    }

    -(IBAction)rightStop {
    [moveRightTimer invalidate];
    [moveRightTimer release];
    }

    -(IBAction)up {
    moveUpTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector

    Please Register or Log in to view images

    selector(moveUp) userInfo:nil repeats:YES];

    }

    -(void)moveUp {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:0.01];
    CGPoint center = (CGPoint) [player center];
    center.y -=1.0f;
    [player setCenter:center];
    [UIView commitAnimations];
    }

    -(IBAction)upStop {
    [moveUpTimer invalidate];
    [moveUpTimer release];
    }

    -(IBAction)home {
    [scaryFace invalidate];
    UINavigationController *navController = self.navigationController;
    RootViewController *home = [[RootViewController alloc] initWithNibName

    Please Register or Log in to view images

    "RootViewController" bundle:nil];
    [navController pushViewController:home animated:YES];
    [home release];
    }



    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


    }

    - (void)viewDidUnload {
    [super viewDidUnload];
    }


    - (void)dealloc {
    [Audioplayer release];
    [super dealloc];
    }


    @end[/OBJC]

    Thanks in advance for your help!

    Please Register or Log in to view images

Share This Page