iPhone SDK:我有内存问题,任何人都可以帮助新手修复它们吗?

时间:2009-06-29 18:07:21

标签: iphone xcode memory-management animation memory-leaks

我一直在iPhone平台上开发很短的时间,而且我已经分享了语法错误和现在的内存问题。

在我的iPhone应用程序中,图像视图通过不同的按钮(6)多次动画,这在iPhone上运行良好没有问题,但是当我第一次用仪器测试我的应用程序时,它找到了1或2个内存泄漏,当调用执行动画的IBAction和它的声音效果时会发生内存泄漏。现在我明白了内存泄漏是什么,但对防止它们知之甚少,

我是否应该担心几个kb内存泄漏,请记住我计划稍后添加更多动画? 将非常感谢帮助。 这是我的代码:

#import "00001111ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@implementation 00001111ViewController


@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize switchView;
@synthesize myScrollView1;
@synthesize img;
- (void)viewDidLoad {

    blinkTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/1.00) target:self selector:@selector(blink) userInfo:nil repeats:NO];
    [blinkTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%10]];
}

- (void)blink{
    [self animateBlink];
}

- (void)animateBlink{

    UIImage *openeye = [UIImage imageNamed:@"img.png"];
    UIImage *closedeye = [UIImage imageNamed:@"cloudnewimprovedblinked.png"];

    if(img.image == openeye)    
        img.image = closedeye;
else if(img.image == closedeye)
        img.image = openeye;

else 
    img.image ==  closedeye;
    blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(blink) userInfo:nil repeats:NO];
if (img.image == openeye)
    [blinkTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%10]];
}
-(IBAction)toggleShowHide2:(id)sender
{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [switchView setAlpha:0];
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
}
-(IBAction)toggleShowHide1:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 1];
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [switchView setAlpha:.88];
    [UIView commitAnimations];
}
- (IBAction)startClick:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound1=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"clickwithsilence" ofType:@"wav"]] error:NULL];
    animationTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed: @"imgapple0001.png"],
                              [UIImage imageNamed: @"imgapple0002.png"],
                              [UIImage imageNamed: @"imgapple0003.png"],
                              [UIImage imageNamed: @"imgapple0004.png"],
                              [UIImage imageNamed: @"imgapple0005.png"],
                              [UIImage imageNamed: @"imgapple0006.png"],
                              [UIImage imageNamed: @"imgapple0007.png"],
                              [UIImage imageNamed: @"imgapple0008.png"],
                              [UIImage imageNamed: @"imgapple0009.png"],
                              [UIImage imageNamed: @"imgapple0010.png"],
                              [UIImage imageNamed: @"imgapple0011.png"],
                              [UIImage imageNamed: @"imgapple0013.png"],
                              [UIImage imageNamed: @"imgapple0014.png"],
                              [UIImage imageNamed: @"imgapple0015.png"],
                              [UIImage imageNamed: @"imgapple0016.png"],
                              [UIImage imageNamed: @"imgapple0017.png"],
                              [UIImage imageNamed: @"imgapple0018.png"],
                              [UIImage imageNamed: @"imgapple0019.png"],
                              [UIImage imageNamed: @"imgapple0020.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =.7;
    [img startAnimating];
    [sound1 play];
}
- (void)tick{
    [self animateYes];
}
- (void)animateYes{

    UIImage *YesImage13=[UIImage imageNamed:@"imgapple0020.png"];


    if(img.image == YesImage13)
        img.image = YesImage13;
    else
        img.image = YesImage13;
    if(img.image == YesImage13)

        blinkTimer2 = [NSTimer scheduledTimerWithTimeInterval:(1.00/1.00) target:self selector:@selector(blink2) userInfo:nil repeats:NO];
    [blinkTimer2 setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%7]];
}

- (void)blink2{
    [self animateBlink2];
}

- (void)animateBlink2{

    UIImage *openeye2 = [UIImage imageNamed:@"imgapple0020.png"];
    UIImage *closedeye2 = [UIImage imageNamed:@"appleblinked.png"];

    if(img.image == openeye2)   
        img.image = closedeye2;
    else if(img.image == closedeye2)
        img.image = openeye2;

    else 
        img.image ==  closedeye2;
    blinkTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(blink2) userInfo:nil repeats:NO];
    if (img.image == openeye2)
        [blinkTimer2 setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%10]];







}
- (IBAction)startClick1:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound2=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"explosiontrimmed" ofType:@"wav"]] error:NULL];
    animationTimer1 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick1) userInfo:nil repeats:NO];
    img.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed: @"img0015.png"],
                              [UIImage imageNamed: @"img0016.png"],
                              [UIImage imageNamed: @"img0017.png"],
                              [UIImage imageNamed: @"img0018.png"],
                              [UIImage imageNamed: @"img0019.png"],
                              [UIImage imageNamed: @"img0020.png"],
                              [UIImage imageNamed: @"img0021.png"],
                              [UIImage imageNamed: @"img0022.png"],
                              [UIImage imageNamed: @"img0023.png"],
                              [UIImage imageNamed: @"img0024.png"],
                              [UIImage imageNamed: @"img0025.png"],
                              [UIImage imageNamed: @"img0026.png"],
                              [UIImage imageNamed: @"img0027.png"],
                              [UIImage imageNamed: @"img0028.png"],
                              [UIImage imageNamed: @"img0029.png"],
                              [UIImage imageNamed: @"img0030.png"],
                              [UIImage imageNamed: @"img0031.png"],
                              [UIImage imageNamed: @"img0032.png"],
                              [UIImage imageNamed: @"img0033.png"],
                              [UIImage imageNamed: @"img0034.png"],
                              [UIImage imageNamed: @"img0035.png"],
                              [UIImage imageNamed: @"img0036.png"],
                              [UIImage imageNamed: @"img0037.png"],
                              [UIImage imageNamed: @"img0038.png"],
                              [UIImage imageNamed: @"img0039.png"],
                              [UIImage imageNamed: @"img0040.png"],
                              [UIImage imageNamed: @"img0041.png"],
                              [UIImage imageNamed: @"img0042.png"],
                              [UIImage imageNamed: @"img0043.png"],
                              [UIImage imageNamed: @"img0044.png"],
                              [UIImage imageNamed: @"img0045.png"],
                              [UIImage imageNamed: @"img0046.png"],
                              [UIImage imageNamed: @"img0047.png"],
                              [UIImage imageNamed: @"img0048.png"],
                              //and so on to 100
                              nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =2.7;
    [img startAnimating];
    [sound2 play];
}
- (void)tick1{
    [self animateYes1];
}
- (void) animateYes1{

    UIImage *YesImage11=[UIImage imageNamed:@"img0048.png"];


    if(img.image == YesImage11)
        img.image = YesImage11;
    else
        img.image = YesImage11;
    if(img.image == YesImage11)

    blinkTimer1 = [NSTimer scheduledTimerWithTimeInterval:(1.00/1.00) target:self selector:@selector(blink1) userInfo:nil repeats:NO];
    [blinkTimer1 setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%7]];
}

- (void)blink1{
    [self animateBlink1];
}

- (void)animateBlink1{

    UIImage *openeye1 = [UIImage imageNamed:@"img0048.png"];
    UIImage *closedeye1 = [UIImage imageNamed:@"explosionblinked.png"];

    if(img.image == openeye1)   
        img.image = closedeye1;
    else if(img.image == closedeye1)
        img.image = openeye1;

    else 
        img.image ==  closedeye1;
    blinkTimer1 = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(blink1) userInfo:nil repeats:NO];
    if (img.image == openeye1)
        [blinkTimer1 setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%7]];
}








- (IBAction)startClick2:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound3=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"watersplashsilence" ofType:@"wav"]] error:NULL];
    animationTimer2 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick2) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:

                              [UIImage imageNamed: @"imgshark0001.png"],
                              [UIImage imageNamed: @"imgshark0002.png"],
                              [UIImage imageNamed: @"imgshark0003.png"],
                              [UIImage imageNamed: @"imgshark0004.png"],
                              [UIImage imageNamed: @"imgshark0005.png"],
                              [UIImage imageNamed: @"imgshark0006.png"],
                              [UIImage imageNamed: @"imgshark0007.png"],
                              [UIImage imageNamed: @"imgshark0008.png"],
                              [UIImage imageNamed: @"imgshark0009.png"],
                              [UIImage imageNamed: @"imgshark0010.png"],
                              [UIImage imageNamed: @"imgshark0011.png"],
                              [UIImage imageNamed: @"imgshark0012.png"],
                              [UIImage imageNamed: @"imgshark0013.png"],
                              [UIImage imageNamed: @"imgshark0014.png"],
                              [UIImage imageNamed: @"imgshark0015.png"],
                              [UIImage imageNamed: @"imgshark0016.png"],
                              [UIImage imageNamed: @"imgshark0017.png"],
                              [UIImage imageNamed: @"imgshark0018.png"],
                              [UIImage imageNamed: @"imgshark0019.png"],
                              [UIImage imageNamed: @"imgshark0020.png"],
                              [UIImage imageNamed: @"imgshark0021.png"],
                              [UIImage imageNamed: @"imgshark0022.png"],
                              [UIImage imageNamed: @"imgshark0023.png"],
                              [UIImage imageNamed: @"imgshark0024.png"],
                              [UIImage imageNamed: @"imgshark0025.png"],
                              [UIImage imageNamed: @"imgshark0026.png"],
                              [UIImage imageNamed: @"imgshark0027.png"],
                              [UIImage imageNamed: @"imgshark0028.png"],
                              [UIImage imageNamed: @"imgshark0029.png"],
                              [UIImage imageNamed: @"imgshark0030.png"],
                              [UIImage imageNamed: @"imgshark0031.png"],
                              [UIImage imageNamed: @"imgshark0032.png"],
                              [UIImage imageNamed: @"imgshark0033.png"],
                              [UIImage imageNamed: @"imgshark0034.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =2.8;
    [img startAnimating];
    [sound3 play];
}
- (void)tick2{
    [self animateYes2];
}
- (void)animateYes2{

    UIImage *YesImage14=[UIImage imageNamed:@"imgshark0034.png"];


    if(img.image == YesImage14)
        img.image = YesImage14;
    else
        img.image = YesImage14;






}
- (IBAction)startClick3:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound4=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"dogsilence" ofType:@"wav"]] error:NULL];

    animationTimer3 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick3) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed: @"imgdog0001.png"],
                              [UIImage imageNamed: @"imgdog0002.png"],
                              [UIImage imageNamed: @"imgdog0003.png"],
                              [UIImage imageNamed: @"imgdog0004.png"],
                              [UIImage imageNamed: @"imgdog0005.png"],
                              [UIImage imageNamed: @"imgdog0006.png"],
                              [UIImage imageNamed: @"imgdog0007.png"],
                              [UIImage imageNamed: @"imgdog0008.png"],
                              [UIImage imageNamed: @"imgdog0009.png"],
                              [UIImage imageNamed: @"imgdog0010.png"],
                              [UIImage imageNamed: @"imgdog0011.png"],
                              [UIImage imageNamed: @"imgdog0012.png"],
                              [UIImage imageNamed: @"imgdog0013.png"],
                              [UIImage imageNamed: @"imgdog0014.png"],
                              [UIImage imageNamed: @"imgdog0015.png"],
                              [UIImage imageNamed: @"imgdog0016.png"],
                              [UIImage imageNamed: @"imgdog0017.png"],
                              [UIImage imageNamed: @"imgdog0018.png"],
                              [UIImage imageNamed: @"imgdog0019.png"],
                              [UIImage imageNamed: @"imgdog0020.png"],
                              [UIImage imageNamed: @"imgdog0021.png"],
                              [UIImage imageNamed: @"imgdog0022.png"],
                              [UIImage imageNamed: @"imgdog0023.png"],
                              [UIImage imageNamed: @"imgdog0024.png"],
                              [UIImage imageNamed: @"imgdog0025.png"],
                              [UIImage imageNamed: @"imgdog0026.png"],
                              [UIImage imageNamed: @"imgdog0026.png"],
                              [UIImage imageNamed: @"imgdog0028.png"],
                              [UIImage imageNamed: @"imgdog0029.png"],
                              [UIImage imageNamed: @"imgdog0030.png"],
                              [UIImage imageNamed: @"imgdog0031.png"],
                              [UIImage imageNamed: @"imgdog0032.png"],
                              [UIImage imageNamed: @"imgdog0033.png"],
                              [UIImage imageNamed: @"imgdog0034.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =2.7;
    [img startAnimating];
    [sound4 play];
}
- (void)tick3{
    [self animateYes3];

}
- (void)animateYes3{

    UIImage *YesImage14=[UIImage imageNamed:@"imgdog0034.png"];


    if(img.image == YesImage14)
        img.image = YesImage14;
    else
        img.image = YesImage14;






}
- (IBAction)startClick4:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound5=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"arrowhitting" ofType:@"wav"]] error:NULL];
    animationTimer4 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick4) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:

                              [UIImage imageNamed: @"imgarrow0001.png"],
                              [UIImage imageNamed: @"imgarrow0002.png"],
                              [UIImage imageNamed: @"imgarrow0003.png"],
                              [UIImage imageNamed: @"imgarrow0004.png"],
                              [UIImage imageNamed: @"imgarrow0005.png"],
                              [UIImage imageNamed: @"imgarrow0006.png"],
                              [UIImage imageNamed: @"imgarrow0007.png"],
                              [UIImage imageNamed: @"imgarrow0008.png"],
                              [UIImage imageNamed: @"imgarrow0009.png"],
                              [UIImage imageNamed: @"imgarrow0010.png"],
                              [UIImage imageNamed: @"imgarrow0011.png"],
                              [UIImage imageNamed: @"imgarrow0012.png"],
                              [UIImage imageNamed: @"imgarrow0013.png"],
                              [UIImage imageNamed: @"imgarrow0014.png"],
                              [UIImage imageNamed: @"imgarrow0015.png"],
                              [UIImage imageNamed: @"imgarrow0016.png"],
                              [UIImage imageNamed: @"imgarrow0017.png"],
                              [UIImage imageNamed: @"imgarrow0018.png"],
                              [UIImage imageNamed: @"imgarrow0019.png"],
                              [UIImage imageNamed: @"imgarrow0020.png"],
                              [UIImage imageNamed: @"imgarrow0021.png"],
                              [UIImage imageNamed: @"imgarrow0022.png"],
                              [UIImage imageNamed: @"imgarrow0023.png"],
                              [UIImage imageNamed: @"imgarrow0024.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =.7;
    [img startAnimating];
    [sound5 play];
}
- (void)tick4{
    [self animateYes4];
}
- (void)animateYes4{

    UIImage *YesImage15=[UIImage imageNamed:@"imgarrow0024.png"];


    if(img.image == YesImage15)
        img.image = YesImage15;
    else
        img.image = YesImage15;






}
- (IBAction)startClick5:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound6=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"imgquote1" ofType:@"wav"]] error:NULL];
    animationTimer5 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick5) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed: @"qotebandb0000.png"],
                              [UIImage imageNamed: @"qotebandb0001.png"],
                              [UIImage imageNamed: @"qotebandb0002.png"],
                              [UIImage imageNamed: @"qotebandb0003.png"],
                              [UIImage imageNamed: @"qotebandb0004.png"],
                              [UIImage imageNamed: @"qotebandb0005.png"],
                              [UIImage imageNamed: @"qotebandb0006.png"],
                              [UIImage imageNamed: @"qotebandb0007.png"],
                              [UIImage imageNamed: @"qotebandb0008.png"],
                              [UIImage imageNamed: @"qotebandb0009.png"],
                              [UIImage imageNamed: @"qotebandb0010.png"],
                              [UIImage imageNamed: @"qotebandb0011.png"],
                              [UIImage imageNamed: @"qotebandb0012.png"],
                              [UIImage imageNamed: @"qotebandb0013.png"],
                              [UIImage imageNamed: @"qotebandb0014.png"],
                              [UIImage imageNamed: @"qotebandb0015.png"],
                              [UIImage imageNamed: @"qotebandb0016.png"],
                              [UIImage imageNamed: @"qotebandb0017.png"],
                              [UIImage imageNamed: @"qotebandb0018.png"],
                              [UIImage imageNamed: @"qotebandb0019.png"],
                              [UIImage imageNamed: @"qotebandb0020.png"],
                              [UIImage imageNamed: @"qotebandb0021.png"],
                              [UIImage imageNamed: @"qotebandb0022.png"],
                              [UIImage imageNamed: @"qotebandb0023.png"],
                              [UIImage imageNamed: @"qotebandb0024.png"],
                              [UIImage imageNamed: @"qotebandb0025.png"],
                              [UIImage imageNamed: @"qotebandb0026.png"],
                              [UIImage imageNamed: @"qotebandb0027.png"],
                              [UIImage imageNamed: @"qotebandb0028.png"],
                              [UIImage imageNamed: @"qotebandb0029.png"],
                              [UIImage imageNamed: @"qotebandb0030.png"],
                              [UIImage imageNamed: @"qotebandb0031.png"],
                              [UIImage imageNamed: @"qotebandb0032.png"],
                              [UIImage imageNamed: @"qotebandb0033.png"],
                              [UIImage imageNamed: @"qotebandb0034.png"],
                              [UIImage imageNamed: @"qotebandb0035.png"],
                              [UIImage imageNamed: @"qotebandb0036.png"],
                              [UIImage imageNamed: @"qotebandb0037.png"],
                              [UIImage imageNamed: @"qotebandb0038.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =3.1;
    [img startAnimating];
    [sound6 play];
}
- (void)tick5{
    [self animateYes5];
}
- (void)animateYes5{

    UIImage *YesImage17=[UIImage imageNamed:@"qotebandb0038.png"];


    if(img.image == YesImage17)
        img.image = YesImage17;
    else
        img.image = YesImage17;

}
- (IBAction)startClick6:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [switchView setAlpha:0];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [myScrollView1 setAlpha: 0];
    [UIView commitAnimations];
    sound7=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audiop" ofType:@"wav"]] error:NULL];
    animationTimer6 = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:@selector(tick6) userInfo:nil repeats:NO];

    img.animationImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed: @"hammer0001.png"],
                              [UIImage imageNamed: @"hammer0002.png"],
                              [UIImage imageNamed: @"hammer0003.png"],
                              [UIImage imageNamed: @"hammer0004.png"],
                              [UIImage imageNamed: @"hammer0005.png"],
                              [UIImage imageNamed: @"hammer0006.png"],
                              [UIImage imageNamed: @"hammer0007.png"],
                              [UIImage imageNamed: @"hammer0008.png"],
                              [UIImage imageNamed: @"hammer0009.png"],
                              [UIImage imageNamed: @"hammer0010.png"],
                              [UIImage imageNamed: @"hammer0011.png"],
                              [UIImage imageNamed: @"hammer0012.png"],
                              [UIImage imageNamed: @"hammer0013.png"],
                              [UIImage imageNamed: @"hammer0014.png"],
                              [UIImage imageNamed: @"hammer0015.png"],
                              [UIImage imageNamed: @"hammer0016.png"],
                              [UIImage imageNamed: @"hammer0017.png"],
                              [UIImage imageNamed: @"hammer0018.png"],
                              [UIImage imageNamed: @"hammer0019.png"],
                              [UIImage imageNamed: @"hammer0020.png"],
                              [UIImage imageNamed: @"hammer0021.png"],
                              [UIImage imageNamed: @"hammer0022.png"],
                              [UIImage imageNamed: @"hammer0023.png"],
                              [UIImage imageNamed: @"hammer0024.png"],
                              [UIImage imageNamed: @"hammer0025.png"],
                              [UIImage imageNamed: @"hammer0026.png"],
                              [UIImage imageNamed: @"hammer0027.png"],
                              [UIImage imageNamed: @"hammer0028.png"],
                              [UIImage imageNamed: @"hammer0029.png"],
                              [UIImage imageNamed: @"hammer0030.png"],
                              [UIImage imageNamed: @"hammer0031.png"],
                              [UIImage imageNamed: @"hammer0032.png"],
                              [UIImage imageNamed: @"hammer0033.png"],
                              [UIImage imageNamed: @"hammer0034.png"],nil];

    [img setAnimationRepeatCount:1];
    img.animationDuration =2.25;
    [img startAnimating];
    [sound7 play];
}
- (void)tick6{
    [self animateYes6];
}
- (void)animateYes6{

    UIImage *YesImage18=[UIImage imageNamed:@"img.png"];


    if(img.image == YesImage18)
        img.image = YesImage18;
    else
        img.image = YesImage18;






}




- (void)stopTimer
{
    [animationTimer invalidate];
    [animationTimer release];
    [animationTimer1 invalidate];
    [animationTimer1 release];
    [animationTimer2 invalidate];
    [animationTimer2 release];
    [animationTimer3 invalidate];
    [animationTimer3 release];
    [blinkTimer invalidate];
    [blinkTimer release];
}







-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    img.center = location; 
}

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

// Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data



- (void)dealloc {
    [super dealloc];
    [img release];
    [sound1 release];
    [sound2 release];
    [sound3 release];
    [sound4 release];
    [sound5 release];
    [sound6 release];
    [sound7 release];
}
@end

为了澄清,定时器用于将最后一帧的每个动画设置为imageView的图像。

6 个答案:

答案 0 :(得分:3)

我可以看到您分配AVAudioPlayer对象的代码可能存在内存泄漏:

sound1=[[AVAudioPlayer alloc] initWithContentsOfURL:...

我可以从您的代码中看到sound#变量是您在-(void)dealloc方法中尽职尽责的实例变量。但是,如果在课程的生命周期内多次启动该特定操作,则会发生泄漏。每次触发操作时,sound1都会获得一个全新的AVAudioPlayer对象,并立即丢失对旧对象的引用。

我建议添加以下内容:

[sound1 release];
sound1 = [[AVAudioPlayer alloc] initWithContentsOfUrl: ...

请务必设置:

sound1 = nil;

使用-(id)init方法,以便第一次拨打[sound1 release]不会造成麻烦。

更新:

防止内存泄漏的经验法则是:每当您alloccopyretain某个对象时,您还必须release它。

你是否担心这里和那里泄漏了几个KB?我会说是的。如果你关心你的工作,你应该尽力使它成为最好的。我不是说你从不发布内存泄漏量小的代码。我说小内存泄漏应该被认为是错误。它们应该显示在“要解决的问题”列表中,您应该尽快将其删除。

对评论的回复:

您的00001111ViewController课程应该有一个名为-(id)init的方法:

- (id)init
{
    if ((self = [super init]) == nil) { return nil; }

    sound1 = nil;
    sound2 = nil;
    //...

    return self;
}

init方法中的代码在创建类的实例时运行,就像在销毁类的实例时调用dealloc方法中的代码一样。请注意,如果您的超类(00001111ViewController继承自的类)使用不同的指定初始值设定项,则应使用该方法名称。在Cocoa世界中,初始值设定项始终以init开头,但它们可能会采用其他参数,例如initWithNibName:bundle:initWithContentsOfFile:

另一次更新:

经过多次考虑之后,或许最好只在AVAudioPlayer方法中分配viewDidLoad个对象一次。

- (void)viewDidLoad
{
    blinkTimer = [NSTimer ...
    [blinkTimer setFireDate: ...

    sound1 = [[AVAudioPlayer alloc] initWithContentsOfUrl: ...
    sound2 = [[AVAudioPlayer alloc] initWithContentsOfUrl: ...
    sound3 = [[AVAudioPlayer alloc] initWithContentsOfUrl: ...
}

完成后,您不再需要每次在startClick方法中重新创建它们。你所要做的就是在这些方法结束时调用[sound1 play],一切都会好的。

答案 1 :(得分:1)

理想情况下,您没有内存泄漏。但你可以选择以这种方式看待它:如果每次点击只丢失几KB,并且不会有太多点击,那么这几乎不是问题。我已经看到仪器报告的内存泄漏代码不是我的(框架代码,也就是),它只有几KB,它只是偶尔发生,所以我认为它没关系。这不是一个好习惯,但浪费时间追踪无法解决的漏洞并不能很好地利用你的时间。

因此,如果您可以确定泄漏不会失控,您可以将其留在那里。但是如果这种泄漏发生了数十次或数百次,最多可以泄漏100多个KB,那么你很快就会发现自己陷入困境。您的资源在iPhone上受限,泄漏是iPhone应用程序崩溃的首要原因。

为了帮助您解决代码问题,我发现Clang Static Analyzer工具非常有用。工具here有一个GUI。它将生成泄漏报告以及代码中需要检查的其他区域。

还有一件事:Objective-C中内存管理的一般规则是,如果您分配,复制或保留对象,则必须将其释放。否则不管它。大多数泄漏都可以通过找到像[[MyObject alloc] init]这样的代码的任何地方来解决,并确保在此之后的某个时间调用[MyObject release]。

答案 2 :(得分:1)

我也是iPhone SDK的新手(一般都是gc-less编程);我发现Clang / LLVM非常有用......这是我使用的指南:

http://www.oiledmachine.com/posts/2009/01/06/using-the-llvm-clang-static-analyzer-for-iphone-apps.html

答案 3 :(得分:0)

对于初学者,您可以使用init方法

创建保存图像的NSArrays

然后你可能会考虑在放入图像时找到一个模式。创建一个循环的函数并将路径作为参数将图像放入数组中。

答案 4 :(得分:0)

第一个答案指出静态分析仪,这绝对是一件好事。另一个好用的是动态分析器,它在运行时监视分配/解除分配和内存访问。 Valgrind是一种流行的开源动态分析器,常见于Linux上。

Valgrind can work on OSX(虽然它说它“不受支持且不完整且有错误”),并且有点骇客,有人让它在iPhone SDK executables上工作。

更好的是你可以尝试仪器,这是XCode的一部分。有一个运行它的教程here

答案 5 :(得分:0)

所以是的,仍有内存泄漏。 3日按