释放什么 [英] What to release

查看:54
本文介绍了释放什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解内存管理方面确实有问题,尽管我以为我明白了.从 plist 导入数据后,我对引用计数的理解完全搞砸了(并花了一整天的时间试图修复它)

I do have a problem with understanding memory management, although I thought I got it. After I imported my data from a plist I got totally messed with my understanding of reference counting (and spend a whole day trying to fix it)

我的 plist 结构基本上就是:

My plist's structure is basically just:

<dict>
<key>Menue1</key>
<array>
    <dict>
        <key>pic1</key>
        <string>a</string>
        ...and so on...
    </dict>
    <dict>
        <key>pic1</key>
        <string>a</string>
        ...and so on...
    </dict>
    <dict>
        <key>pic1</key>
        <string>a</string>
        ...and so on...
    </dict>
</array>
<key>Menue2</key>
<array>
    <dict>
        <key>pic1</key>
        <string>a</string>
        ...and so on...
    </dict>
    <dict>
        <key>pic</key>
        <string></string>
        <key>text</key>
        <string></string>
    </dict>
</array>
</dict>

在代码中,我通过

//to get the path
path = [[NSBundle mainBundle] pathForResource:@"plistsName" ofType:@"plist"]; 

//to get the root dictionary from path      
NSDictionary *tempdict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    dict = tempdict;
// [tempdict release]; <-- app crashes when I release this

// to get the Array of dicts inside of the dict "Menue1"
exercises = [dict objectForKey:@"Menue1"]; 

// to get the dictionary containing the detailed informations
exerciseViewContent = [[NSDictionary alloc] initWithDictionary:[exercises objectAtIndex:0]];

我确实理解引用计数的概念(我认为是这样),但我显然没有通过基本数学.

I do understand the idea of reference counting (I think so), but I apparently fail basic maths.

由于 @interface 我想参考如下:

Due to the @interface I suppose the references been as followed:

dict = 1
exercises = 1
exerciseViewContent = 1

viewDidLoad之后,我在上面做所有这些事情,我认为它就像

After viewDidLoad, where I do all this stuff above i assume it to be like

dict = 1
exercises = 1
exerciseViewContent = 2
tempdict = 1

所以一开始我想我必须在 viewDidLoadtempdict 中释放一次 exerciseViewContent,然后释放所有三个(dict、extraction、extractionViewContent) 在 dealloc 中使它们都等于 0. 摆弄了一段时间后,我发现我必须释放 exerciseViewContentdealloc 中,但不是 exercisedict(我确实将它们的属性设置为保留在界面中并合成它们),但是完全不需要释放 tempdict 超出了我的理解,因为我肯定使用了 alloc .只需摆弄它现在就可以工作(直到现在).

So at FIRST I thought I'd have to release exerciseViewContent once inside viewDidLoad, as well as tempdict, and release all three (dict, exercise, exerciseViewContent) in the dealloc to get all of them to equal 0. After fiddling quite a while I found out that I have to release exerciseViewContent in dealloc, but not exercise or dict (i do set their properties as retained in the interface and synthesize them though), but to not need to release tempdict totally is more than I understand, as I definitely used alloc with it. Just by fiddling it works now (till now).

所以我想知道:

  • 首先:我的内存管理是否正确?
  • 例如什么时候dict 发布了,因为我从来没有发布过它(或者我的应用程序崩溃了)?
  • 这是从 plist 访问字典和数组的正确方法吗?
  • 我假设 initWithContentsOfFile 包括自动释放,但我怎么知道呢?是否有包含 autorelease 的方法列表(或不包含 autorelease 的方法列表,哪个会更短)?
  • 我希望有一个实际可行的经验法则,因为每个 alloc 或 init 都需要一个版本"显然不适合这里.
  • First of all: is my memory management correct like this?
  • When is e.g. dict released, as I never release it (or my App crashes)?
  • Is this the proper way of accessing dicts and arrays from a plist?
  • I assume initWithContentsOfFile includes autorelease, but how should I know that? Is there a list of methods that include autorelease (or a list that doesn't, what ever would be shorter)?
  • I'd like to have a rule of thumb that actually works out, because "every alloc or init needs a release" apparently doesn't fit here.

另外,我需要释放路径"吗?

Further, do I need to release "path"?

还有:

[timer invalidate];
timer = nil;

与发布相同,因为我显然也不必发布计时器(NSTimer),尽管我将其设置为保留?还是因为它是一个不需要释放的 NSTimer,就像 NSInteger 一样?我可以在类引用中的哪个位置看到默认情况下必须发布哪些类型,哪些没有?

the same as a release, as I apparently also don't have to release timer (NSTimer), although I set it's property to retain? Or is this because it's a NSTimer which doesn't need to be released, just like NSInteger? And where in the class reference can I see which type has to be released and which hasn't by default?

还有:

  • (来自 Xcode 模板:)

  • (from the Xcode Template:)

(void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

如果我将myOutlet"的属性设置为保留在界面中,我还需要释放它吗?

Will I still have to release "myOutlet" if I set it's property to retain in the interface?

推荐答案

变量 dict 和 tempDict 是指向对象的指针,而不是对象本身.所以当你这样做时:

The variables dict and tempDict are pointers to objects, not the object(s) themselves. So when you do this:

dict = tempDict;

您实际上只是为 相同 对象设置了别名.将 release 发送到 tempDict 也将它发送到 dict.这就是你崩溃的原因.您释放了一个刚刚分配的对象,因此您不再拥有它.对象被释放,留下一个悬空指针.

you have really only set an alias to the same object. Sending release to tempDict is also sending it to dict. This is why you get the crash. You have released an object you only just allocated and therefore you no longer own it. The object is deallocated leaving dict a dangling pointer.

我的内存管理是否正确?

is my memory management correct like this?

如果没有 [tempDict release],在一定程度上是的.完成后,您需要释放 dict 和 ExerciseViewContent 因为它们是通过 alloc 获得的.

Without the [tempDict release], up to a point yes. When you are done with them, you need to release both dict and exerciseViewContent because you obtained them with an alloc.

例如什么时候dict 已发布,因为我从未发布它(或我的应用程序崩溃)?

When is e.g. dict released, as I never release it (or my App crashes)?

正如我上面所说的,tempDict 指向同一个对象.释放 tempDict 并释放 dict.

As I said above, tempDict points to the same object. Release tempDict and you release dict.

这是从 plist 访问字典和数组的正确方法吗?

Is this the proper way of accessing dicts and arrays from a plist?

没关系,只是我会像这样获得运动视图内容:

It's OK except I would just obtain exerciseViewContent like this:

exerciseViewContent = [exercises objectAtIndex:0];

那么你不需要释放它.

假设 initWithContentsOfFile 包括 autorelease,但我怎么知道?

assume initWithContentsOfFile includes autorelease, but how should I know that?

不,没有.重要的一点是对象是通过 alloc 获得的,这意味着你拥有它.你应该如何通过理解 Cocoa 内存管理规则.

No, it doesn't. The important point is that the object was obtained with alloc which means you own it. How you should know it is by understanding the Cocoa Memory Management Rules.

我想要一个实际可行的经验法则,因为每个 alloc 或 init 都需要一个版本"显然不适合这里.

I'd like to have a rule of thumb that actually works out, because "every alloc or init needs a release" apparently doesn't fit here.

是的,它确实适合.你只是误解了这行的意思:

Yes, it does fit. You were just misunderstanding the meaning of the line:

dict = tempDict;

这篇关于释放什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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