使用AppDelegate变量作为全局变量 - 关于释放/保留的问题 [英] Using Variable of AppDelegate as a Global Variable - question regarding release/retain

查看:120
本文介绍了使用AppDelegate变量作为全局变量 - 关于释放/保留的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AppDelegate中创建了一个名为myDBManager的变量:

  @interface myAppDelegate:NSObject< UIApplicationDelegate> {
MyDBManager * myDBManager;
}
@property(nonatomic,retain)MyDBManager * myDBManager;

@end

它在大多数其他类中用作全局变量保存我所有的关键应用数据。它只能创建一次,只会在最后死亡。例如,要在AnyOtherClass中访问myDBManager

  @interface AnyOtherClass:UITableViewController {
MyDBManager * myDBManager;
NSObject * otherVar;
}
@property(nonatomic,retain)MyDBManager * myDBManager;
@property(nonatomic,retain)NSObject * otherVar;
@end

//从全局myDBManager中获取数据并将其放入AnyOtherClass的局部变量
- (void)viewWillAppear:(BOOL)animated {
//获取myDBManager全局对象
MyAppDelegate * mainDelegate =(MyAppDelegate *)[[UIApplication sharedApplication] delegate];
myDBManager = mainDelegate.myDBManager;
}


- (void)dealloc {
[otherVar release];
// [dancesDBManager发布];不要将其作为全球变量使用!
[super dealloc];





$ b

这是我的问题:虽然AnyOtherClass的所有其他局部变量,例如 otherVar必须在AnyOtherClass的dealloc方法中发布(always-是吗?),在AnyOtherClass中释放myDBManager会导致应用程序出错。所以我永远不会释放本地myDBManager变量在我的应用程序的任何类 - 所有其他本地变量总是释放 - 它工作正常。 (甚至检查retainCount)。



我是对的,所有类的局部变量都需要在该类的dealloc中释放,或者它确实可以,不是在使用所描述的全局变量构造的情况下是否释放这些变量? (或任何其他情况?)



非常感谢您的帮助!

解决方案

在您的场景中, myDBManager 不是全局变量或局部变量,它是一个自动保留的属性(标记为 retain )。根据 Objective-C编程语言:声明属性 nonatomic,retain 属性应该在 dealloc 中显式释放。但是,如果你的属性是综合的,你不能访问后备成员变量,因此你不能对它调用 release ;您必须使用属性设置器将其设置为 nil ,这会将 release 发送到上一个值。您是否有机会在 AnyOtherClass myAppDelegate myDBManager 属性c> to nil



更新:@Don的答案实际上是正确的。你没有调用属性设置器( self.myDBManager ),所以自动保留不会启动。我将离开我的答案,以便人们可以从我的错误中学习。 : - )

I have created a Variable called "myDBManager" in my AppDelegate:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
   MyDBManager *myDBManager;
}
@property (nonatomic, retain)  MyDBManager *myDBManager;

@end

which I use in most other classes as a global Variable holding all my critical application data. It gets only created once and dies at the end only. So for example to get to the myDBManager in AnyOtherClass

@interface AnyOtherClass :  UITableViewController {
   MyDBManager *myDBManager;
   NSObject *otherVar;
}
@property (nonatomic,retain)   MyDBManager *myDBManager;
@property (nonatomic,retain)   NSObject *otherVar;
@end

//getting the data from "global" myDBManager and putting it into local var of AnyOtherClass
- (void)viewWillAppear:(BOOL)animated {
   //get the myDBManager global Object
   MyAppDelegate *mainDelegate = (MyAppDelegate *)[[UIApplication sharedApplication]delegate];
   myDBManager = mainDelegate.myDBManager;
   }


-  (void)dealloc {
       [otherVar release];
        //[dancesDBManager release]; DO NOT RELEASE THIS SINCE ITS USED AS A GLOBAL VARIABLE!
        [super dealloc];
        }

Here is my question: while all other local variables of AnyOtherClass, such as "otherVar" have to be released in the dealloc method of AnyOtherClass (always- is that right?), releasing myDBManager within AnyOtherClass leads to errors in the app.

So I NEVER release the local myDBManager variable in any Class of my app - all other local variables are always released - and it works fine. (Even checking the retainCount).

Am I right, that ALL local variables of classes need to be released in the dealloc of that class, or is it actually OK, not to release these variables at all in cases of using a global variable construct as described? (or any other cases?)

Thanks very much for your help!

解决方案

In your scenario myDBManager is not a global or a local variable, it's an auto-retained property (marked with retain). According to The Objective-C Programming Language: Declared Properties, nonatomic,retain properties should be explicitly released in dealloc. However, if your property is synthesized, you don't have access to the backing member variable, thus you can't call release on it; you have to use the property setter to set it to nil, which will send release to the previous value. Are you by any chance setting the myDBManager property in AnyOtherClass and myAppDelegate to nil?

Update: @Don's answer is actually correct. You are not invoking the property setter (self.myDBManager), so the auto-retain does not kick in. I'll leave my answer so people can learn from my mistake. :-)

这篇关于使用AppDelegate变量作为全局变量 - 关于释放/保留的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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