Singleton issue causing a buffer overrun
- by Rudiger
Hi everyone,
Ive created a singleton to store 2 arrays:
@interface Globals : NSObject {
NSMutableArray *items;
NSMutableArray *extras;
}
+ (Globals *)sharedInstance;
@property (nonatomic, assign) NSMutableArray *items;
@property (nonatomic, assign) NSMutableArray *extras;
@end
@implementation Globals
@synthesize items, extras;
+ (Globals *)sharedInstance {
static Globals *myInstance = nil;
@synchronized(self) {
if(!myInstance) {
myInstance = [[Globals alloc] init];
}
}
return myInstance;
}
-(void)dealloc {
[items release];
[extras release];
[super dealloc];
}
@end
When I set the Arrays in the singleton from the App delegate and then output them to NSLog it displays what is expected. But when I call it from a view controller further into the App it displays the first entry fine, some of the second entry and then garbage which is i assume a buffer overrun, sometimes it also crashes.
I set the singleton array in the appDelegate like so:
Globals *sharedInstance = [Globals sharedInstance];
[sharedInstance setItems:items];
and retrieve it:
[[[sharedInstance items] objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.description.text = [[[sharedInstance items] objectAtIndex:indexPath.row] objectForKey:@"description"];
Name works fine in both cells if there is 2, description works in the first case, never in the second case.
Is it because the arrays in my singleton aren't static? If so why is it outputting the first entry fine?
Cheers for any help.