When I check a singleton property that is nil, it kills my app. But when I check for nil on a class instance property, everything works fine.
This works fine:
self.MyProperty == nil
but this will kill the app with “EXC_BAD_ACCESS”
[MySingleton sharedManager].SomeProperty != nil
What is the difference with the singleton that I can't check for nil?
Here's the singleton implementation:
.h file:
@interface MySingleton : NSObject {
NSString * SomeProperty;
}
@property (nonatomic, copy) NSString * SomeProperty;
+(MySingleton *)sharedManager;
@end
.m file:
#import "MySingleton"
static MySingleton *sharedManager = nil;
@implementation MySingleton
@synthesize SomeProperty;
- (void)dealloc {
[SomeProperty dealloc];
[super dealloc];
}
+(MySingleton *)sharedManager
{
if (!sharedManager){
sharedManager = [[MySingleton alloc] init];
}
return sharedManager;
}
This is what I find in the console when when trying to assign something to SomeProperty:
MyApp(51363,0xa0389500) malloc: *** mmap(size=2147487744) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug Current language: auto; currently objective-c (gdb) break malloc_error_break Note: breakpoints 6 and 8 also set at pc 0x929c2072. Breakpoint 11 at 0x929c2072 (gdb) continue (gdb) po [MySingleton sharedManager].SomeProperty Cannot access memory at address 0x0 (gdb) po [MySingleton sharedManager] <Session: 0x1938fa0>
I get the above only when trying to assign. When trying to read the variable is where the crash occurs.