为什么“自我"保护内存空间? [英] Why does 'self' protect memory space?

查看:80
本文介绍了为什么“自我"保护内存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于导航的应用程序中,我在不使用self的情况下在我的应用程序委托中初始化了一个数组.当在RootViewController的cellForRowAtIndexPath:中访问时,所有数组对象都在那里,我可以看到它是一个NSCFArray.应用加载后,我单击一个表格单元格,然后在didSelectRowAtIndexPath:中,该数组具有NSArray类型,没有对象,并且出现EXC_ BAD_ACCESS错误.如果我在应用程序委托中在数组前加self,那么一切都很好.为什么会这样?

In a navigation based app, I initialize an array in my app delegate without using self. When accessed in the RootViewController's cellForRowAtIndexPath:, all array objects are there and I can see it is an NSCFArray. Once the app loads, I click a table cell and in didSelectRowAtIndexPath:, that same array has a type of NSArray, no objects and I get a EXC_ BAD_ACCESS error. If I precede the array with self in the app delegate, all is fine. Why is that?

下面是应用程序委托.h文件:

Below is the app delegate .h file:

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

  UIWindow *window;
  UINavigationController *navigationController;
  NSMutableDictionary *aDict;
  NSArray *aArray;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSArray *aArray;
@property (nonatomic, retain) NSArray *aDict
@end

数组和字典在.m文件中合成.我在应用程序委托.m文件中初始化数组,如下所示:

The array and dictionary are synthesized in the .m file. I initialize the array in the app delegate .m file like this:

aArray = [self.aDict allKeys];

在两个根控制器方法中都可以这样访问它:

It is accessed like this in both root controller methods:

theDelegate = [[UIApplication sharedApplication] delegate]; 
[theDelegate.aArray objectAtIndex:2];

只有当我到达didSelectRowAtIndexPath时,它才会失败.在应用程序委托中执行此操作可使一切正常:

Only when I reach didSelectRowAtIndexPath: does it fail. Doing this in the app delegate makes everything work:

self.aArray = [self.aDict allKeys];

在cellForRowAtIndexPath:和didSelectRowAtIndexPath:之间,我对myArray不做任何事情.为什么在第一种情况下失败?

I didn't do anything to myArray between cellForRowAtIndexPath: and didSelectRowAtIndexPath:. Why does it fail in the first scenario?

推荐答案

这是因为self.aArray使用了综合属性.您将该属性定义为(nonatomic, retain),这意味着无论将其设置为什么,该值都将保留.当您使用此功能时,会发生allKeys被自动释放的情况,而当您实际使用它时,它已经消失了.使用该属性会保留该值,使其超出其范围.

That is because self.aArray makes use of the synthesized property. You defined the property as (nonatomic, retain), this means that whatever it is set to, that value will be retained. What was happening when you didn't use this was that the allKeys was being auto-released, and by the time you actually used it, it was gone. Using the property retained that value, making it live past its scope.

希望如此,对不起,如果我误解了你.

Hope that made sense, sorry if I misunderstood you.

进一步阅读:

  • Apple Docs - Memory Management Rules
  • Apple Docs - Declared Properties
  • Apple Docs - NSAutoreleasePool
  • Question Regarding How NSAutoreleasePool Works

我敢肯定还有更多对读者友好的文章,但是现在您知道了要查找的内容:Objective-C内存管理和Objective-C属性.这非常重要,因此我建议您这样做.

I'm sure there are more reader friendly articles out there, but now you know what to look up: Objective-C memory management, and Objective-C properties. It's pretty important so I recommend you do this.

这篇关于为什么“自我"保护内存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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