【问题标题】:iphone UIImage array memory leakiphone UIImage 数组内存泄漏
【发布时间】:2010-08-10 11:41:52
【问题描述】:

我之前有过这个问题,尝试过回答,但还是不行。

在我分配之前:

imageArray_danceright =  [[NSArray alloc] initWithObjects:

我被建议这样做,但现在它在调用第二个动画后立即崩溃:


//init areas:

imageArray_stand = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankieSingtRetime_0001" ofType:@"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankieSingtRetime_0002" ofType:@"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankiePeaceRetime_0003" ofType:@"jpg"]],nil];


imageArray_danceleft = [NSArray arrayWithObjects: 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]   pathForResource:@"FrankiePeaceRetime_0001" ofType:@"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankiePeaceRetime_0040" ofType:@"jpg"]],nil];

imageArray_danceright = [NSArray arrayWithObjects: 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankiePeaceRetime_0041" ofType:@"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FrankiePeaceRetime_0080" ofType:@"jpg"]],nil];


//start:
[myimageview setAnimationImages:imageArray_stand];


myimageview.animationDuration = 0.23;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0.0;
myimageview.image = [myimageview.animationImages objectAtIndex:1];
[myimageview startAnimating];


// ------ in my 'touchesBegan" i have:

if ([touch view] == overlay_fuesserechts) { 
    [myimageview.animationImages release];
    [myimageview setAnimationImages:imageArray_danceright];

    myimageview.animationDuration = 2.0;
    myimageview.contentMode = UIViewContentModeBottomLeft;
    myimageview.animationRepeatCount = 0;
    [myimageview startAnimating];
}


- (void)dealloc {
[imageArray_stand release];
imageArray_stand=nil;
[imageArray_danceright release];
imageArray_danceright=nil;
[imageArray_danceleft release];
imageArray_danceleft=nil;       
    super dealloc];
 }

它以“站立”动画开始..但是当我触发 touchesbegan 时它立即崩溃:

    [myimageview setAnimationImages:imageArray_danceright];

我不知道。我尝试了很多东西。现在我希望你有一个想法。

谢谢 克里斯

【问题讨论】:

  • 不..这就是问题..它只是停止。我在 [myimageview setAnimationImages:imageArray_danceright] 之前和之后做了一个 NSLog;出于这个原因,我知道它在那里崩溃:)

标签: iphone memory-management


【解决方案1】:

因为您正在自动释放图像数组,所以当它到达 TouchesBegan 时,它们已经被释放了。 谁告诉你使用 arrayWithObjects 而不是 alloc - initWithObjects ? 听起来您的早期版本会更正确。你遇到了什么问题?

【讨论】:

  • 在它工作得很好之前......问题是它没有释放内存,所以在第 4 个动画(我可以在 Instruments/Allocation 中看到)之后它得到了 100MB 并崩溃了。如果您愿意提供帮助,我什至会再次发布我的旧代码:)
  • [myimageview.animationImages release]; 在我看来也是错误的。我会这样做myimageview.animationImages = nil;
  • 事实上,此时应该是多余的 - setAnimationImages 应该释放它已经持有的东西。可能值得在 dealloc 中这样做。这个对象是否也拥有 myimageview?我没有看到你发布那个
  • 谢谢......我在这里发布了我的旧代码:stackoverflow.com/questions/3448911/… 对于这个问题......你是什么意思“这个对象是否也拥有 myimageview”哪个对象。以上是我发布的所有内容.. 你可以让我知道还有什么要发布的 :) 如上所述,旧代码位于上面的链接中。再次感谢。顺便提一句。我是否可以直接与您联系..我什至会将整个代码发送给您...如果我找不到解决方案,我必须使用计时器和没有动画进行测试:(
【解决方案2】:

您肯定不需要在开始触摸时使用[myimageview.animationImages release];。您正在创建自动释放的数组对象并将它们分配给释放它们的图像视图。

【讨论】:

  • ok.. 我删除了 [myimageview.animationImages 版本];但它仍然只是在 [myimageview setAnimationImages:imageArray_danceright];被解雇了??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多