Reputation: 21
(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
@property (nonatomic, strong) IBOutlet UILabel *lblResultado;
@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
@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
}
Upvotes: 1
Views: 2615
Reputation: 21
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)?????
Upvotes: 0
Reputation: 5190
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).
Upvotes: 3
Reputation: 125007
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
.
Upvotes: 0
Reputation: 945
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.
Upvotes: -1
Reputation: 945
Try initializing your singleton using the static + (void)initialize
method instead.
See: What should my Objective-C singleton look like?
Upvotes: 0