ios - Possibilities of Singleton instance getting deallocated automatically -
i creating singleton instance below:
+(mysingleton*)sharedinstance { static mysingleton sharedobject = nil; static dispatch_once_t predicate = 0; dispatch_once(&predicate, ^{ sharedobject = [[mysingleton alloc] init]; }); return sharedobject; }
what possibilities of sharedobject
getting deallocated automatically?
how can sure sharedobject
remain in memory until app terminated?
as answer rightly points out, shared singleton never deallocated. there 2 parts answer "why", both of come following line:
static mysingleton * sharedobject = nil;
first, static
. static
, when used inside of function this, modifies lifetime of variable, changing automatic
, implicit default, static
. means variable exists entire lifetime of program.
second, declaration makes sharedobject strong reference. variables in objective-c strong default, pedantic, have written:
static __strong mysingleton * sharedobject = nil;
so: have variable lives entire duration of program (static
), , maintains strong reference object represents (__strong
). these 2 pieces of information, plus fact never change object variable points, can deduce sharedobject
never deallocated.
Comments
Post a Comment