Singleton issue causing a buffer overrun

Posted by Rudiger on Stack Overflow See other posts from Stack Overflow or by Rudiger
Published on 2010-05-12T22:49:43Z Indexed on 2010/05/12 22:54 UTC
Read the original article Hit count: 193

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about singleton