我有一个使用此行加载的NSMutableArray(timeZoneTree)
timeZoneTree = [[Timezone getTimeZoneTree] retain];
时区的定义是
@interface Timezone : NSObject
{
NSString *name;
int rawOffset;
}
和getTimeZoneTree方法如下:
+ (NSMutableArray*) getTimeZoneTree
{
if (offsetGroups == nil)
{
// initialize a new mutable array
offsetGroups = [[[NSMutableArray alloc] init] autorelease];
// build the path to the timezones file
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TimeZones.dat"];
// get the data of the file
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
BufferReader *reader = [[BufferReader alloc] initWithBuffer:(const char*)[data bytes] length:[data length]];
OffsetGroup *curOffsetGroup;
RegionGroup *curRegionGroup;
int offsetGroupCount = [reader readInt32];
// go through all raw offset groups
for(int curOffsetGroupIndex=0; curOffsetGroupIndex<offsetGroupCount; ++curOffsetGroupIndex)
{
int rawOffset = [reader readInt32];
// initialize a new offset group
curOffsetGroup = [[OffsetGroup alloc] init];
curOffsetGroup.rawOffset = rawOffset;
// and add it to the offset groups
[offsetGroups addObject:curOffsetGroup];
int regionGroupCount = [reader readInt32];
// go through all region group of the war offset group
for(int curRegionGroupIndex=0; curRegionGroupIndex<regionGroupCount; ++curRegionGroupIndex)
{
int regionNameLength = [reader readInt8];
char *regionNameUTF8 = malloc(regionNameLength + 1);
[reader readBytes:regionNameUTF8 withLength:regionNameLength];
regionNameUTF8[regionNameLength] = '\0';
// initialize a new region group
curRegionGroup = [[RegionGroup alloc] init];
curRegionGroup.name = [NSString stringWithCString:regionNameUTF8 encoding:NSUTF8StringEncoding];
// and add it to the region groups of the offset group
[curOffsetGroup.regionGroups addObject:curRegionGroup];
int timeZoneCount = [reader readInt32];
// go through all time zones
for(int curTimeZoneIndex=0; curTimeZoneIndex<timeZoneCount; ++curTimeZoneIndex)
{
int timeZoneNameLength = [reader readInt8];
char *timeZoneNameUTF8 = malloc(timeZoneNameLength+1);
[reader readBytes:timeZoneNameUTF8 withLength:timeZoneNameLength];
timeZoneNameUTF8[timeZoneNameLength] = '\0';
// create a new time zone name
NSString *timeZoneName = [NSString stringWithCString:timeZoneNameUTF8 encoding:NSUTF8StringEncoding];
// if the name is not nil
if (timeZoneName != nil)
// then add it to the region group
[curRegionGroup.timeZones addObject:timeZoneName];
free(timeZoneNameUTF8);
}
[curRegionGroup release];
free(regionNameUTF8);
}
[curOffsetGroup release];
}
[reader release];
[data release];
}
return offsetGroups;
}
有什么问题?为什么代码有时会崩溃?我正在使用ROOTSDK 7.0进行构建
答案 0 :(得分:1)
我会说autorelease
似乎是一个全局变量:
offsetGroups = [[[NSMutableArray alloc] init] autorelease];
// ^^^^^^^^^^^
这意味着一旦线程命中自动释放池漏(可能在运行循环中),阵列就会被释放。
删除autorelease
的使用,如果您这样做,则需要删除第一行代码上的retain
。