让我抓狂!动画图像总是显示内存泄漏

发布于 2024-08-14 19:29:36 字数 897 浏览 3 评论 0原文

我一定已经尝试过该代码的十几种变体。并查看了+100 个答案。它应该如此简单。一个真正的基本动画(这次使用 IB),但总是显示内存泄漏。无论我把我的零、我的释放放在哪里,都尝试了无数种组合。

// 实现 viewDidLoad 在加载视图后进行额外的设置(通常是从笔尖加载)。

- (void)viewDidLoad {
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

     myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                         [UIImage imageNamed:@"2.png"],
                                                         [UIImage imageNamed:@"3.png"],
                                                         [UIImage imageNamed:@"4.png"],nil];

    myView.animationDuration = 1.0;

    myView.animationRepeatCount = 0;

    [myView startAnimating];

    [myView release];
     myView = nil;

    [myView.animationImages release];


    NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);
}

I must have tried over a dozen incarnations of this code. And looked at +100 answers. IT SHOULD BE so simple. A real basic animation, (using IB this time) but ALWAYS shows a memory leak. NO matter where I put my nil, my release, tried a zillion combinations.

// implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

     myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                         [UIImage imageNamed:@"2.png"],
                                                         [UIImage imageNamed:@"3.png"],
                                                         [UIImage imageNamed:@"4.png"],nil];

    myView.animationDuration = 1.0;

    myView.animationRepeatCount = 0;

    [myView startAnimating];

    [myView release];
     myView = nil;

    [myView.animationImages release];


    NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2024-08-21 19:29:36

这里有各种各样的问题。您不拥有您试图在此处发布的任何参考文献。您看到的实际泄漏是什么?模拟器还是设备?

首先清理这段代码,删除你的版本并将设置设置为零。

There are all kinds of issues here. You don't own either of the references you're trying to release here. What is the actual leak you're seeing? Simulator or Device?

Start by cleaning up this block of code, getting rid of your releases and your setting to nil.

最初的梦 2024-08-21 19:29:36

您的问题没有提供有关图像数组和 myView 所发生情况的足够详细信息。由于无法看到全貌,我建议您尝试“构建”->“构建”。清理所有目标然后构建 ->在 Xcode 中构建和分析。此构建和分析只是 Xcode Snow Leopard 版本中的一个选项。它将分析代码是否存在潜在的内存泄漏,并可能帮助您指明正确的方向。它不会涵盖所有内容,但它确实有助于指出一些我在匆忙时容易错过的事情。

否则,我建议您发布 myView 的分配位置以及animationImages 的属性声明。

Your question isn't giving enough detail of the whole picture of what is going on with your images array and myView. Without being able to see what's going on in the full picture I would suggest that you try Build -> Clean All Targets and then Build -> Build and Analyze in Xcode. This Build and Analyze is only an option in the Snow Leopard version of Xcode. It will analyze code for potential memory leaks and might help point you in the right direction. It won't pick up on everything, but it does help point out a few things that I tend to miss when I'm in a rush.

Otherwise, I suggest that you post where myView is being allocated and the property declaration of animationImages.

咿呀咿呀哟 2024-08-21 19:29:36

我不太确定我是对还是错。但我不认为 myView.animationImages 需要被释放,因为它之前没有被初始化,除非它在程序的其他部分被初始化,我认为这是极不可能的。

请对内存管理更了解的人验证一下。谢谢你!

I'm not too sure whether i'm right or wrong. but i don't think myView.animationImages needs to be release since it is not init before unless it was init in other parts of your program which i think is highly improbable.

Someone out there who is more knowledgeable in memory management please verify. Thank you!

小耗子 2024-08-21 19:29:36

您是否尝试过将 [myView.animationImages release]; 放在 [myView release]; 之前?释放 myView 后,您将无法再访问 myView.animationImages,因为没有任何内容引用它。

Have you tried putting [myView.animationImages release]; before [myView release];? Once you release myView, you can't access myView.animationImages anymore, since there's nothing referencing it.

秋凉 2024-08-21 19:29:36

添加

myView.animationImages = nil;

之前:

myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                     [UIImage imageNamed:@"2.png"],
                                                     [UIImage imageNamed:@"3.png"],
                                                     [UIImage imageNamed:@"4.png"],nil];

因此,最终代码如下:

myView.animationImages = nil;

myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                     [UIImage imageNamed:@"2.png"],
                                                     [UIImage imageNamed:@"3.png"],
                                                     [UIImage imageNamed:@"4.png"],nil];

myView.animationDuration = 1.0;

myView.animationRepeatCount = 0;

[myView startAnimating];

[myView release];
myView = nil;

Add

myView.animationImages = nil;

before :

myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                     [UIImage imageNamed:@"2.png"],
                                                     [UIImage imageNamed:@"3.png"],
                                                     [UIImage imageNamed:@"4.png"],nil];

Hence, final code to be like this :

myView.animationImages = nil;

myView.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"],
                                                     [UIImage imageNamed:@"2.png"],
                                                     [UIImage imageNamed:@"3.png"],
                                                     [UIImage imageNamed:@"4.png"],nil];

myView.animationDuration = 1.0;

myView.animationRepeatCount = 0;

[myView startAnimating];

[myView release];
myView = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文