ios application crashes randomly

I have an NSMutableArray (timeZoneTree) that is loaded using this line

timeZoneTree = [[Timezone getTimeZoneTree] retain];

The definition of Timezone is

@interface Timezone : NSObject 
    NSString *name;
    int rawOffset;

and the getTimeZoneTree method is the following:

+ (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];
       = [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];

                [curRegionGroup release];
            [curOffsetGroup release];
        [reader release];
        [data release];

    return offsetGroups;

What is the problem? Why the code crashed sometimes on this line? I am building with ROOTSDK 7.0


I'd say it was the use of autorelease in what appears to be an global variable:

offsetGroups = [[[NSMutableArray alloc] init] autorelease];
//                                            ^^^^^^^^^^^

This means as soon as the thread hits an autorelease pool drain (in the run loop, probably) the array is released.

Remove the use of autorelease, and if you do this you need to remove the retain on the first line of code.

Need Your Help

How to output error message if user inputs numbers out of range?

javascript if-statement range

My objective is to not allow the user to enter a weight that is &lt; 0 or > 400.If they do, output an error message in red to div#results and ‘return’ out of the function. I set an if statement but...

Error in compiling the kernel

linux linux-kernel linux-device-driver

I am trying to compile linux 3.5.2 kernel. I followed this tutorial