Hi all.
Accessing NSDate in singleton crashes, with Bad_Access!!
note: accessing any other parts of the singleton works fine. So not included.
SingletonCentre.h
SingletonCentre.m
in Gettit.m
** Also it seems to only crash when initializing of if I run it a few times it works.. WT???
thanks, for any help!
Ian
Accessing NSDate in singleton crashes, with Bad_Access!!
note: accessing any other parts of the singleton works fine. So not included.
SingletonCentre.h
Code:
@interface SingletonCentre : NSObject
{
NSDate *lastUpdate;
}
@property (nonatomic, retain) NSDate *lastUpdate;
+ (SingletonCentre *)sharedSingleton;
@end
SingletonCentre.m
Code:
// Note this is a fragment so I might have missed a bracket or two //
#import "SingletonCentre.h"
@implementation SingletonCentre
static SingletonCentre *shared;
@synthesize lastUpdate;
- (id)init
{
if ( self = [super init] )
{
NSData *HSEncodedObject = [NSKeyedArchiver
archivedDataWithRootObject:self.myHighScoreKeeperSingleton];
NSString *dateString = @"02-Aug-04";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd-MMM-yy";
lastUpdate = [dateFormatter dateFromString:dateString ];
[def setObject:lastUpdate forKey:@"lastUpdate"];
NSLog(@"lastupdate set to: %@",lastUpdate);
[def synchronize];
}else {
self.lastUpdate = [def objectForKey:@"lastUpdate"];
}
return self;
}
+ (SingletonCentre *)sharedSingleton
{
@synchronized(shared)
{
if ( !shared||shared==NULL)
{
// allocate the shared instance, because it hasn't been done yet
shared = [[SingletonCentre alloc] init];
NSLog(@"created Singleton");
}
return shared;
}
}
- (void)dealloc
{
NSLog(@"Deallocating singleton...");
[super dealloc];
}
-(void)saveSingletons
{
[def setObject:lastUpdate forKey:@"lastUpdate"];
NSLog(@"Single: date %@",lastUpdate);
}
@end
in Gettit.m
Code:
- (void)doStuff
{
SingletonCentre *sharedHSData = [SingletonCentre sharedSingleton];
//** Code fragment that crashes with EXC_BAD_ACCESS (code1, address =
NSLog(@"sing: %@",sharedHSData.lastUpdate); // <---- this line -----
NSLog(@"sing: %@",[sharedHSData lastUpdate]);// <----or this -------
}
// more stuff
@end
** Also it seems to only crash when initializing of if I run it a few times it works.. WT???
thanks, for any help!
Ian
Last edited: