C样式块会导致内存泄漏吗? [英] Can C style blocks cause memory leaks?

查看:75
本文介绍了C样式块会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用信息亭风格的幻灯片应用程序.我有一个UIScrollView显示幻灯片,还有一个工厂类,生成幻灯片. 幻灯片"本身是UIViewController子类,可从XIB文件中加载并由工厂类自定义.在主视图控制器中,设置滚动视图并启动计时器.计时器每N秒调用一次重载"方法,该方法处理重载并调用工厂类.

I'm working on a kiosk style slideshow app. I have a UIScrollView which shows the slides, and a factory class, which generates the slides. The "slides" themselves are UIViewController subclasses, which are loaded out from XIB files and customized by the factory class. In my main view controller, I set up the scroll view and start a timer. The timer calls a "reload" method every N seconds, which handles the reload and call to the factory class.

工厂类使用的方法如下:

The method that the factory class uses looks something like this:

- (SlideViewController *)slideFromManagedObject:(Slide *)managedObject{

  NSInteger slideType = [managedObject slideType];

  switch(slideType){
     case kSlideTypeA:

  { 
       //
       //  configure arguments here
       //

       return [[SlideViewController alloc] initWithArgument:argument] autorelease];
         break;

  }

    //
    //  More types here...
    //

    default:
      break;
  }


}

我还没有定义所有案例,但是填写的案例似乎导致

I haven't yet gotten to the point of defining all of my cases, but the ones that are filled out seem to cause jumps in memory usage. If I add return [[[UIViewController alloc] init] autorelease]; right before the switch/case, I get no visible view, as expected, but I also don't see those memory increases. I'm not sure, but I suspect that it's the "C blocks" that I'm wrapping my slide generation code in.

一些注意事项:

  • 当应用启动时,我看到内存从大约400 KB增加到原来的两倍.然后,当幻灯片前进时,其生成代码包含在花括号中的任何幻灯片都将被调用,存储平台将再次向上稳定.

  • When the app starts, I see the memory plateau from about 400 kilobytes to around double that. Then, when the slides progress, any of the slides whose generation code is contained in curly braces is called, the memory plateaus upwards again.

此行为似乎仅在每次启动时发生一次-当应用程序循环浏览所有幻灯片时,高原to_not_再次发生. 但是,如果应用程序在后台运行,然后重新启动,则高原确实会再次发生,甚至会消耗更多的 内存.

This behavior only seems to happen once per launch - when the app loops through all of the slides, the plateaus to_not_ happen again. However if the app is backgrounded and then relaunched, the plateaus do occur again, consuming even more memory.

当我离开应用程序运行整夜(大约10小时40分钟)时,内存使用量已从大约1.44兆字节缓慢攀升至接近1.57兆字节的水平.我怀疑那里/是否有其他泄漏可能已通过我的调整得到解决,但是从大约800 KB到1.4到1.5 MB之间的主要跃迁仍然是一个问题.

When I left the app to run overnight, for about 10 hours and forty minutes, the memory usage had slowly climbed from about 1.44 megabytes to somewhere closer to 1.57 megabytes. I suspect that there are/were some other leaks in there that may have been fixed by my tweaking, but the main jump from about 800 kilobytes to somewhere between 1.4 and 1.5 megabytes is still an issue.

仪器没有报告任何泄漏,但是平稳期使我担心.

Instruments does not report any leaks, but the plateauing concerns me.

什么原因导致内存增加?

What could be causing the increased memory?

所以我不认为这是障碍,因为使用if/else似乎做同样的事情. 这是运行分配"工具的屏幕截图:

So I don't think it's the blocks, since using an if/else seems to do the same thing. Here's a screenshot of the Allocations instrument running:

在哪里可以保留这些观点?

Where could possibly be holding on to these views?

推荐答案

关于您所看到内容的一种可能解释是UIKit(我认为)正在处理您的对象(不知道它们是什么)的某种缓存.大多想到图片).

One possible explanation for what you are seeing is some caching that UIKit (I assume) is doing of your objects (don't know what they are, but I think of images mostly).

缓存通常用于过渡期间以及UIKit的其他内部功能.

Caching is often used during transitions and for other internalities of UIKit.

UIKit通常在收到内存警告时清空其缓存,因此您可以尝试发送一个以查看会发生什么.实际上,我怀疑发送内存警告的结果不是很容易分析,因为所有视图也都已卸载,因此内存将被强制关闭.但是你可以尝试...

UIKit empties its caches usually when a memory warning is received, so you could try and send one to see what happens. In actuality, I suspect that results of sending a memory warning will not be very easy to analyze, since all of your views are also unloaded, hence memory will go down forcibly. But you can try...

关于如何向设备(而不是模拟器)发送内存警告,在这里您可以找到有用的

As to how sending a memory warning to the device (as opposed to the simulator), here you find an useful S.O. post.

这篇关于C样式块会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆