Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You're autorleasing the array, and not retaining it anywhere. It's probably getting released, so you're crashing.
Create a retained property, and save the array in the property using property notation.
Hi there.
The reason why its autoreleased is because its in a function where I need to return the array, and I believe that I need to autorelease it otherwise it would add a memory footprint for it.
Here is the full code to the method/funciton
Code:
// Inside a view controller
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.listOfPlayers = [[NSMutableArray alloc] init];
Player *p = [[Player alloc] init];
self.listOfPlayers = [p getAllPlayers];
NSLog(@"Results -- %@", self.listOfPlayers);
// This is where it will crash
NSLog(@"Count -- %@", [self.listOfPlayers count]);
[p release];
}
// Inside player.m
-(NSMutableArray*)getAllPlayers
{
bootleggers3AppDelegate *appDelegate = (bootleggers3AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *listOfPlayers = [[[NSMutableArray alloc] init] autorelease];
NSDictionary *dict;
[appDelegate.db beginTransaction];
FMResultSet *rs = [appDelegate.db executeQuery:@"SELECT p.id AS playerID, p.fname AS playerFName, p.sname AS playerSName, p.playerAvatar AS playerAvatar, a.name AS avatarName FROM players AS p INNER JOIN avatars AS a ON p.playerAvatar = a.id ORDER BY p.id ASC LIMIT 4"];
// Loop through results
while ([rs next])
{
NSNumber *plyID = [NSNumber numberWithInt:[rs intForColumn:@"playerID"]];
NSString *plyFName = [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerFName"]];
NSString *plySName = [NSString stringWithFormat:@"%@", [rs stringForColumn:@"playerSName"]];
NSNumber *plyAvatar = [NSNumber numberWithInt:[rs intForColumn:@"playerAvatar"]];
NSString *avName = [NSString stringWithFormat:@"%@", [rs stringForColumn:@"avatarName"]];
// Put the above into a dictionary
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"plyID", plyID,
@"plyFName", plyFName,
@"plySName", plySName,
@"plyAvatar", plyAvatar,
@"avName",avName,
nil];
[listOfPlayers addObject:dict];
} // wend
// Close the result set.
[rs close];
[appDelegate.db commit];
return listOfPlayers;
}
I will try your idea of a property notation and retaining on a cut down version of the above in a new file and seeing if that works instead.