I have used Singleton for a long time, but today I find a strange things that the singleton object will 'autorelease'? And will cause the App crash next time try to call singleton object.
Finally I find that because I raise new thread in this class. With normal singleton class definination:
+ (DataAccessConroller *)sharedController {
@synchronized([DataAccessConroller class]) {
if (!_sharedController) {
_sharedController=[[self alloc] init];
}
return _sharedController;
}
return nil;
}
/*
The Methods should not changed.
*/
+ (id)alloc {
@synchronized([DataAccessConroller class]) {
_sharedController = [super alloc];
return _sharedController;
}
return nil;
}
- (id)init {
self = [super init];
if (self) {
xxxx
}
return self;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized([DataAccessConroller class]) {
_sharedController= [super allocWithZone:zone];
return _sharedController;
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
//- (void)release {
// //do nothing
//}
- (id)autorelease {
return self;
}
- (void)dealloc {
xxxx
[super dealloc];
}
When I try to contruct a new thread to do sth:
[NSThread detachNewThreadSelector:@selector(xxx) toTarget:self withObject:nil];
It will call retain method, and when the thread end it will call dealloc automatically
Bcz the we have redefine the retain method, that will cause the problem
- (id)retain {
return self;
}
So, my solution is remove the retain defination, and normally we will not retain or alloc a singleton object.
That's all.
//************************************************************
I met a error of muti-thread today:
reason: '-[__NSCFType isValid]: unrecognized selector sent to instance
I trager a timer and set to a local var, but next time I user the timer will raise upper error, from debug I know the timer is exist and never be release. But the loop function never be called. That make me think out a previous problem, if raise a timer in a thread the timer will not effect. That's the reason. And even cause crash if isValid is called.
分享到:
相关推荐
DataSource的单态模式(SingleTon)程序---马克-to-win java视频 数据源 单态模式 dataSource Singleton
$this->app->singleton('cache.multi-memcached', function ($app) { $stores = []; foreach ($app['config']['cache.stores'] as $store => $config) { if ($config['driver'] === 'memcached') { $connector ...
perl-Class-Singleton-1.4-6.el6.noarch.rpm perl-Class-Singleton-1.4-6.el6.noarch.rpm
private static final Singleton INSTANCE = new Singleton(); } private Singleton() {} public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } ``` 上述代码是典型的双检锁/...
python库。 资源全名:django-singleton-admin-0.0.2.macosx-10.7-intel.exe
在`singleton-pattern-master`这个压缩包中,可能包含了一个示例项目或者一个库,它演示了如何在Laravel中实现和使用单例模式。可能的目录结构包括源代码文件、配置文件、示例测试等,帮助开发者更好地理解和应用...
Bean配置它提供2个不同的单例ObjectMapper实例。 参见master-module/BeansConfiguration 为了验证它们是否为单例,请在module-1运行测试并检查打印的输出。多模块设置一个空的父模块,其中包含3个相互依赖的模块。...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
离线安装包,亲测可用
官方离线安装包,亲测可用
以下是对"单例实现源码singleton-C++"的详细解析。 1. **静态成员变量法** 这是最常见的单例实现方式。在类中定义一个静态成员变量,该变量保存唯一的实例。例如: ```cpp class Singleton { public: static ...
在iOS开发中,Singleton(单例)是一种常见的设计模式,用于确保一个类在整个应用程序中只有一个实例存在,并提供全局访问点。Singleton模式的核心在于控制类的实例化过程,防止多个实例的产生,通常用于管理共享...
例如,Singleton Scope 保证在整个应用程序中只有一个服务实例,而 Request Scope 则为每个请求创建新的服务实例。 2. **创建多模块项目**: - 分解项目:根据功能或业务逻辑将项目拆分为多个模块,每个模块有自己...
**PyPI 官网下载 | dask-actor-singleton-1.1.0.tar.gz** 在Python的世界里,PyPI(Python Package Index)是官方的软件仓库,它为开发者提供了发布和分享Python软件包的平台。`dask-actor-singleton-1.1.0.tar.gz`...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. **饿汉式(静态代码块)** 类加载时初始化...
/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/singleton/my.cnf --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/singleton/data --user=root & ``` 2. **启动MySQL服务**...
### C++中实现Singleton模式的关键知识点 #### 一、Singleton模式简介 Singleton模式是一种常用的软件设计模式,旨在确保一个类只有一个实例,并提供一个全局访问点。这种模式在系统中经常被用于控制对共享资源...