Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IDMah

macrumors 6502
Original poster
May 13, 2011
317
11
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
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:
I would recommend distilling your code down to the smallest possible reproducible test case.

You may also wish to research some useful environment variables related to memory allocations and debugging:

OBJC_PRINT_EXCEPTIONS=YES
NSZombieEnabled=YES
MallocStackLogging
MallocGuardEdges
MallocScribble

Good luck :)
 
You're not using ARC, are you? Not with that 'retain' in the property declaration.
And you are setting the ivar directly without retaining it.

Use the setter or retain the ivar.
 
Thanks.

Thanks.
You are correct !! without arc.

so changed in singleton.m
Code:
           //NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] retain]; // <- do I need this ??? it works without.. 
            dateFormatter.dateFormat = @"dd-MMM-yy";
            //lastUpdate =  [dateFormatter dateFromString:dateString ]; From
            self.lastUpdate =  [dateFormatter dateFromString:dateString ]; // To

Seems stable now.. Thanks !!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.