1

(CODE UPDATED) after IOS 7 update, my app is crashing with singleton label (and this happened only in the device)...first time accessing singleton everything it's ok, but second time ClassSingleton is nil. Can anyone help? (before IOS 7 everything was fine...now i get Bad Access Code =1)

I'm using ARC...

thanks

ClassSingleton.h

@property (nonatomic, strong) IBOutlet UILabel *lblResultado;

ClassSingleton.m

@synthesize lblResultado;


__strong static ClassSingleton *pOutClassSingletonReturn = nil;



#pragma mark Singleton Methods


+ (void)initialize
{
        pOutClassSingletonReturn = [[super allocWithZone:NULL] init];

        pOutClassSingletonReturn.lblResultado = [[UILabel alloc] init];
        pOutClassSingletonReturn.lblResultado.backgroundColor = [UIColor clearColor];
        pOutClassSingletonReturn.lblResultado.textColor = [UIColor whiteColor];
        pOutClassSingletonReturn.lblResultado.textAlignment = NSTextAlignmentRight;

        pOutClassSingletonReturn.lblResultado.text = @"0";
}



+ (ClassSingleton*) pOutClassSingletonReturn
{
    return pOutClassSingletonReturn;
}

@end

AccessClass.m

@implementation AccessClass

__strong static ClassSingleton *pOutClassSingletonReturn;



- (void)viewDidLoad
{
    [super viewDidLoad];

    externalsObjects = [NSDictionary dictionaryWithObject:[ClassSingleton  pOutClassSingletonReturn] forKey:@"pOutClassSingletonReturn"];


    nibOptions = [NSDictionary dictionaryWithObject:externalsObjects forKey:UINibExternalObjects];

    [self.nibBundle loadNibNamed:self.nibName owner:self options:nibOptions];

    pOutClassSingletonReturn = [ClassSingleton pOutClassSingletonReturn];

    pOutClassSingletonReturn.lblResultado.text = @"1";
}


- (IBAction) button: (id) sender

{
     pOutClassSingletonReturn.lblResultado.text = @"blabla";  //==>>> Crash second time i press the button
}
user2635936
  • 21
  • 1
  • 4
  • 1
    The idea of making a singleton UILabel seems a little weird to me, but I assume you have a reason for it. You must be adding the label as a subview to some view. Presumably, when the view is removed, it removes the label. The second time you attempt to get your singleton, the dispatch_once block doesn't fire again and so you get the null reference. Perhaps if you could post more details about what you are trying to accomplish, somebody could suggest a better approach. – Matthew Burke Sep 20 '13 at 21:00
  • The label changes to "blabla" ever time i press a button (and the second time a press button, i get a bad access because singleton is nil), like that: - (IBAction) button: (id) sender { pOutClassSingletonReturn.lblResultado.text = @"blabla"; } – user2635936 Sep 20 '13 at 21:40

5 Answers5

3

You implemented the singleton pattern improperly in modern Objective-C.

In this example, let's call your Singleton class method, sharedInstance. Initialize your singleton as follows:

+ (id)sharedInstance
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

Then, in the same Singleton Class, put your initialization code in your init method

- (id)init
{
    self = [super init];
    if (self) {

        // Your initialization code goes here

    }
    return self;
}

You can change the method from init to whatever you want. Just make sure to change the name in the sharedInstance class method.

To call your Singleton in your other classes, simply do the following:

[MySingletonClass sharedInstance]

The first time it's called, the init method in the Singleton will be set (which is obvious, as it's a Singleton).

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
0

Try initializing your singleton using the static + (void)initialize method instead.

See: What should my Objective-C singleton look like?

Community
  • 1
  • 1
dtrotzjr
  • 928
  • 5
  • 18
  • i did like you said and continues crashing + (void)initialize { static BOOL initialized = NO; if(!initialized) { initialized = YES; pOutClassSingletonReturn = [[ClassSingleton alloc] init]; } } – user2635936 Sep 20 '13 at 05:35
  • Bummer. Same crash? BTW the static `initialize` method is guaranteed to be called only once when a class object is created. Therefore you don't need the `if(!initialized)` logic, it doesn't harm anything its just not needed. – dtrotzjr Sep 20 '13 at 14:51
  • yes, same crash. First time accessing singleton everything ok, then second time bad access (singleton is nil). Any ideas? i don't understand why only in IOS7 and why only in the device... – user2635936 Sep 20 '13 at 19:35
0

I don't see where you've declared pOutClassSingletonReturn in AccessClass.m, so it's hard to tell if it's a global variable, an instance variable, a reference to the pOutClassSingletonReturn in ClassSingleton.m, or what. But I suspect that the problem isn't so much with the pOutClassSingletonReturn in ClassSingleton.m as it is with the one in AccessClass.m. Make sure that's a strong reference, or at least add it to your view hierarchy in -viewDidLoad.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

First i want to thanks to everyone who try to help!

I found the error (singleton was ok...)...the error was

when i concatenate 2 NSString's like that i get an error (further in the singleton):

pOutclassCalculadora.pstrOutParcela1 = [pOutclassCalculadora.pstrOutParcela1 stringByAppendingString: pOutclassCalculadora.pstrOutTeclaSender];

Now i'm doing like that and everything is ok (no bad access):

pOutclassCalculadora.pstrOutParcela1 = [NSString stringWithFormat:@"%@%@",pOutclassCalculadora.pstrOutParcela1,pOutclassCalculadora.pstrOutTeclaSender];

The big question...why this "stringByAppendingString" works in the simulator and in IO6 and crashes in IOS7 (and only in the device)?????

user2635936
  • 21
  • 1
  • 4
-1

If you are using ARC then try declaring the static to be strong like this:

__strong static ClassSingleton *pOutClassSingletonReturn = nil;

so that ARC knows to retain it for you.

dtrotzjr
  • 928
  • 5
  • 18
  • With __strong same thing...crashes...I tried with instruments ("profile") simulation in the DEVICE and did Ok. But when i use "run" to simulate in device crashes again. Is this a IOS7 Bug?? my app is crashing and i can not see how i'm going resolve this issue :( – user2635936 Sep 20 '13 at 20:12
  • I use singletons in iOS 7 and none of my singletons are problematic for me. Have you tried turning on Zombies? – dtrotzjr Sep 20 '13 at 22:38
  • I am glad you got it working but why did you down vote my answer? – dtrotzjr Sep 21 '13 at 23:11
  • Not me...i can not down vote or vote up, because i don't have "reputation" – user2635936 Sep 22 '13 at 19:58