Objective c Singleton dealloc从未被调用
我的程序启动并存在,但不触发解除锁定Objective c Singleton dealloc从未被调用,objective-c,macos,singleton,Objective C,Macos,Singleton,我的程序启动并存在,但不触发解除锁定 @interface Printer : NSObject + (instancetype)instance; -(void)print; @end @implementation Printer + (instancetype)instance { static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceTo
@interface Printer : NSObject
+ (instancetype)instance;
-(void)print;
@end
@implementation Printer
+ (instancetype)instance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
-(void) print {
printf(@"Printer printing...\n");
}
-(void)dealloc {
printf(@"Printer dealloc\n");
}
@end
int main (int argc, const char * argv[])
{
Printer* tmp = [Printer instance];
[tmp print];
}
我得到以下输出:
Printer printing...
Program ended with exit code: 0
根据输出,dealloc
从未被调用。dealloc内的断点永远不会被触发,即使在我的控制台应用程序被触发时也是如此
你知道怎么了吗?
我相信这是由于
静态
共享状态
变量造成的,但是,不确定该怎么办。单例永远不会被销毁。如果希望析构函数工作,请动态分配和释放内存。这并不是回答我的问题。单身汉不需要被主动摧毁,这一点我很清楚。但是,它们应该在某个时候被销毁,我假设这是问题存在的顺序。不,它们永远不会被释放。没有这样的代码。当你的应用程序完成执行时,操作系统只是将应用程序内存标记为空闲。这对我来说没有多大意义。如果此单例打开了网络连接怎么办?文件已经打开了吗?动态分配的内存?可能重复的