iPhone CGRectMake内存消耗

use*_*816 2 iphone cocoa memory-leaks objective-c

在iPhone上..为什么这样的代码会导致内存泄漏?2分钟后,净字节数翻了一番.我正在做的就是用NSTimer调用以下方法在屏幕上移动一个球.

有任何想法吗?

- (void)nextFrame:(NSNotification *)notification {
    ballInstance.frame = CGRectMake(value, 0,  320, 480);
}
Run Code Online (Sandbox Code Playgroud)

这里是'完整'代码,新项目,仍然表现相同.它会在屏幕上移动jpg,因为它会大量消耗内存.如果我从'value'中删除'++',那么内存就没问题了.(换句话说有一个静态图形)所以....是缓存的图像是问题吗?如果是这样,我怎么能阻止它达到天文尺寸?

- (void)applicationDidFinishLaunching:(UIApplication *)application {    


    [window makeKeyAndVisible];

    NSTimer * nSTimer =[NSTimer scheduledTimerWithTimeInterval: .02
         target: self
       selector: @selector(tick)
       userInfo: nil
        repeats: YES];
    value =0;
}

- (void)tick {
    NSLog(@"tick");
    myOutlet1.frame = CGRectMake(value++, 0,  320, 480);
}
Run Code Online (Sandbox Code Playgroud)

Nik*_*uhe 8

发布的代码没有泄漏.问题出在其他地方.

如果您知道其中存在泄漏nextFrame:,则必须处于此状态,-[Ball setFrame:]因为它是此方法中发送的唯一消息.

  • @unknown:正如Nikolai所说,泄漏必须在你正在调用的setFrame:方法中的某个地方,因为它肯定不在CGRectMake()中. (2认同)