Resolved NSDate singleton.

Discussion in 'iOS Programming' started by IDMah, Mar 17, 2014.

  1. IDMah, Mar 17, 2014
    Last edited: Mar 18, 2014

    IDMah macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #1
    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
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    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 :)
     
  3. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    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.
     
  4. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #4
    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 !!!
     

Share This Page