我相信内存管理存在问题。我尝试分配第二次时收到“EXC_BAD_ACCESS”错误(运行代码并进行第一次分配但第二次使其死亡)。
我有一个函数,它将获取一组自定义对象(包含日期和名称变量)。我需要将这个数组排序到月份特定的数组(所有的aprils将转到aprilList,可能转到mayList等)。
.h文件
@class SceneData;
@interface DetailedSceneViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *mainTableView;
SceneData *sceneData;
NSArray *monthsArray;
}
@end
.m文件
#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];
}
}
}
我是否错误地处理了sceneData对象?
答案 0 :(得分:2)
您没有正确初始化数组:
NSMutableArray *janList, *febList, *marList, *aprList, *mayList, *junList, *julList, *augList, *sepList, *octList, *novList, *decList = [[NSMutableArray alloc] init];
此语法仅初始化最后一个指针...您应该使用:
NSMutableArray *janList = [[NSMutableArray alloc] init];
NSMutableArray *febList = [[NSMutableArray alloc] init];
...
NSMutableArray *decList = [[NSMutableArray alloc] init];
另一方面,所有这些对象都在泄漏,因为(如果我在代码中没有遗漏某些内容)它们是局部变量(不是自动释放的),你不会在它们上面调用release ...