【发布时间】:2010-07-08 22:06:32
【问题描述】:
我在 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
//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];
}
这是我的问题:虽然 AnyOtherClass 的所有其他局部变量,例如“otherVar”,都必须在 AnyOtherClass 的 dealloc 方法中释放(总是 - 对吗?),但在 AnyOtherClass 中释放 myDBManager 会导致应用程序出错.
所以我永远不会在我的应用程序的任何类中释放本地 myDBManager 变量 - 所有其他局部变量总是被释放 - 它工作正常。 (甚至检查 retainCount)。
我是对的,类的所有局部变量都需要在该类的 dealloc 中释放,或者实际上可以,在使用所描述的全局变量构造的情况下根本不释放这些变量? (或任何其他情况?)
非常感谢您的帮助!
【问题讨论】:
标签: xcode global-variables release