Reputation: 3
Having an issue with memory management i believe. i receive a "EXC_BAD_ACCESS" error when it tries to assign the second time (runs the code and makes the first assignment but the second time makes it die).
I have a function which will take an array of custom objects (contains a date and name variables). i need to sort this array into month specific arrays (all the aprils will go to aprilList, may into mayList, etc).
.h file
@class SceneData;
@interface DetailedSceneViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *mainTableView;
SceneData *sceneData;
NSArray *monthsArray;
}
@end
.m file
#import "SceneData.h"
- (void) sortMonths {
Agri_ImaGIS_iPhoneAppDelegate *dataCenter = (Agri_ImaGIS_iPhoneAppDelegate *) [[UIApplication sharedApplication] delegate];
NSMutableArray *janList, *febList, *marList, *aprList, *mayList, *junList, *julList, *augList, *sepList, *octList, *novList, *decList = [[NSMutableArray alloc] init];
int count = [dataCenter.sortedDataList count];
NSLog(@"count: %d", count);
for (int i = 0; i < count; i++)
{
sceneData = [dataCenter.sortedDataList objectAtIndex:i];
if ([sceneData.name rangeOfString:@"Jan" ].location != NSNotFound)
{
NSLog(@"jan name: %@", sceneData.name);
[janList addObject: sceneData];
}
else if ([sceneData.name rangeOfString:@"Feb" ].location != NSNotFound)
{
NSLog(@"feb name: %@", sceneData.name);
[febList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Mar" ].location != NSNotFound)
{
NSLog(@"Mar name: %@", sceneData.name);
[marList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Apr" ].location != NSNotFound)
{
NSLog(@"Apr name: %@", sceneData.name);
[aprList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"May" ].location != NSNotFound)
{
NSLog(@"May name: %@", sceneData.name);
[mayList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Jun" ].location != NSNotFound)
{
NSLog(@"Jun name: %@", sceneData.name);
[junList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Jul" ].location != NSNotFound)
{
NSLog(@"Jul name: %@", sceneData.name);
[julList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Aug" ].location != NSNotFound)
{
NSLog(@"Aug name: %@", sceneData.name);
[augList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Sep" ].location != NSNotFound)
{
NSLog(@"Sep name: %@", sceneData.name);
[sepList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Oct" ].location != NSNotFound)
{
NSLog(@"Oct name: %@", sceneData.name);
[octList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Nov" ].location != NSNotFound)
{
NSLog(@"Nov name: %@", sceneData.name);
[novList addObject:sceneData];
}
else if ([sceneData.name rangeOfString:@"Dec" ].location != NSNotFound)
{
NSLog(@"Dec name: %@", sceneData.name);
[decList addObject:sceneData];
}
}
}
am i handling the sceneData object incorrectly?
Upvotes: 0
Views: 137
Reputation: 69027
You are not initializing correctly your arrays:
NSMutableArray *janList, *febList, *marList, *aprList, *mayList, *junList, *julList, *augList, *sepList, *octList, *novList, *decList = [[NSMutableArray alloc] init];
This syntax will only initialize the last pointer... You should use:
NSMutableArray *janList = [[NSMutableArray alloc] init];
NSMutableArray *febList = [[NSMutableArray alloc] init];
...
NSMutableArray *decList = [[NSMutableArray alloc] init];
On another note, all those object are leaking, since (if I am not missing something in your code) they are local variables (not autoreleased) and you don't call release on them...
Upvotes: 2