0 votes
in Education by (1.7m points)
I have a class that logs on to a server and retrieves some data. The data is put in a NSMutableArray, converted to a NSArray and returned. The code is called like this:

1. NSLog(@"Filling tempoArray");

2. NSArray *tempoArray = [myLogin getRoster:[self employmentnumber] online:YES];

3. NSLog(@"Setting calendarItems with tempoArray");

In the 'myLogin' object, the array is created like this:

4. NSMutableArray *calendarItems = [[NSMutableArray alloc]init];

5. Filling the array...

6. NSArray *tempoArray2 = [[NSArray alloc]initWithArray:calendarItems]; //Converting from mutabe to a normal array

7. NSLog(@"tempoArray2 filled with calendarItems. tempoArray2.count:%d", tempoArray2.count);

8. return tempoArray2;

The NSLog in lines 1 and 7 are written to the console but line 3 is never seen, so it crashes in line 2.

I wonder if I the tempoArray2 is released to soon? And if so, how can I avoid that with ARC?

The error only appears when I download the app from AppStore, I have never seen any problem with this in any debug scenario. How can that be? This is why I use the console log to pinpoint the error. The symbolication in XCode isn't working very good.

EDIT Here is 'line 5'

It is parsing a web page.

    NSString *heleTeksten = [[NSString alloc] initWithData:self.rosterurlmutabledata encoding:NSISOLatin1StringEncoding];

    //NSLog(@"heleteksten%@", heleTeksten);

    NSArray  *lines = [heleTeksten componentsSeparatedByString:@"\n"];

    NSString *searchFor = @"<span class='param";    

    NSString *param1date;

    NSString *param2duty;

    NSString *param3activity;

    NSString *param5checkIn;

    NSString *param6from;

    NSString *param7std;

    NSString *param8sta;

    NSString *param9to;

    int firstPlace;

    NSRange firstRange, secondRange, thirdRange;

    for (__strong NSString* line in lines)

    {

        firstRange = [line rangeOfString:searchFor];

        if (firstRange.location != NSNotFound)

        {

            thirdRange = [line rangeOfString:@"style=\"Padding-top"];

            if (thirdRange.location == NSNotFound) {

                //NSLog(@"New line: %@", line);

                if ([line rangeOfString:@"<B>"].location != NSNotFound) {

                    line = [self removeBfromLine:line];

                }

                if ([line rangeOfString:@";"].location != NSNotFound) {

                    line = [line stringByReplacingOccurrencesOfString:@";" withString:@""];

                }

                secondRange = [line rangeOfString:@"style='color: '>"];

                if (secondRange.location != NSNotFound) {

                    firstPlace = secondRange.location + 16;

                }

                NSString *partOfLine;

                partOfLine = [line substringFromIndex:firstPlace];

                secondRange = [partOfLine rangeOfString:@"<"];

                if (secondRange.location != NSNotFound)

                {

                    //NSLog(@"partOfLine = [partOfLine substringToIndex:secondRange.location] Line:%@", line);

                    partOfLine = [partOfLine substringToIndex:secondRange.location];

                }

                else {

                    //TODO: Endre errorLog her til myErrorReport:

                    //                        [self errorLog:[@"'<' not found in partofline. Line: " stringByAppendingString:line]];

                }

                partOfLine = [partOfLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

                if ([partOfLine length]>0 || [line rangeOfString:@"param11"].location != NSNotFound)

                {                   

                    if ([line rangeOfString:@"'param1'"].location != NSNotFound )

                        // Date-line

                    {

                        param1date = nil;

                        param1date = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param7"].location != NSNotFound)

                        // STD-line

                    {

                        param7std = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param8"].location != NSNotFound)

                        // STA-line

                    {

                        param8sta = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param5"].location != NSNotFound)

                        // CheckIn-line

                    {

                        param5checkIn = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param6"].location != NSNotFound)

                        // Orig-line

                    {

                        param6from = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param9"].location != NSNotFound)

                        // Dest-line

                    {

                        param9to = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param2"].location != NSNotFound)

                        // Duty-line

                    {

                        if ([userdefaults boolForKey:@"inklDutyCodes"])

                        {

                            param2duty = [NSString stringWithString:partOfLine];

                        }

                    }

                    else if ([line rangeOfString:@"param3"].location != NSNotFound)

                        // FlightNo-line

                    {

                        param3activity = [NSString stringWithString:partOfLine];

                    }

                    else if ([line rangeOfString:@"param11"].location != NSNotFound)

                        // Last line. Save calendarItem

                    {

                        calendarItem *newCalendarItem = [[calendarItem alloc]init];

                        //                        currentDate = [self makeNormalDate:partOfLine forAirport:param6from];

                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

                        if (param7std == nil)   // = whole day event

                        {

                            [newCalendarItem setWholeDay:YES];

                            [newCalendarItem setSubject:param3activity];

                            if (param6from == nil)

                            {

                                [newCalendarItem setStartTime:[self makeNormalDate:param1date forAirport:@"OSL"]];

                            }

                            else

                            {

                                [newCalendarItem setStartTime:[self makeNormalDate:param1date forAirport:param6from]];

                            }

                            [newCalendarItem setEndTime:newCalendarItem.startTime];

                        }

                        else

                        {

                            if (param2duty != nil)

                            {

                                [newCalendarItem setDuty:[NSString stringWithString:param2duty]];

                            }

                            else

                            {

                                [newCalendarItem setDuty:[NSString stringWithString:[NSString string]]];

                            }

                            [newCalendarItem setWholeDay:NO];

                            if (param6from != nil)

                                [newCalendarItem setOrig:param6from];

                            else

                                [newCalendarItem setOrig:[NSString string]];

                            if (param9to != nil)

                                [newCalendarItem setDest:param9to];

                            else

                                [newCalendarItem setOrig:[NSString string]];

                            if (param3activity != nil)

                            {

                                if ([param3activity integerValue])  //Sjekker at det her kun er tall

                                {

                                    [newCalendarItem setSubject:[@"SK" stringByAppendingString:param3activity]];

                                }

                                else

                                {

                                    [newCalendarItem setSubject:param3activity];

                                }

                            }

                            else

                            {

                                [newCalendarItem setSubject:[NSString string]];

                            }

                            if (param7std != nil)

                            {

                                if ([param7std length] == 4)

                                {

                                    NSString *dateAndTimeString = [param1date stringByAppendingString:param7std];

                                    NSLocale * enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

                                    [dateFormatter setLocale:enUSPOSIXLocale];

                                    NSTimeZone *thisTimeZone;

                                    if (param6from != nil)

                                    {

                                        thisTimeZone = [self timeZoneForAirport:param6from];

                                    }

                                    if (thisTimeZone == nil)

                                    {

                                        thisTimeZone = [NSTimeZone timeZoneWithName:@"Europe/Oslo"];

                                    }

                                    [dateFormatter setTimeZone:thisTimeZone];

                                    [dateFormatter setDateFormat:@"ddMMMyyHHmm"];

                                    [newCalendarItem setStartTime:[dateFormatter dateFromString:dateAndTimeString]];

                                    if (param5checkIn != nil)

                                    {

                                        calendarItem *checkInCalendarItem = [[calendarItem alloc]init];

                                        NSString *checkinTimeString = [param1date stringByAppendingString:param5checkIn];

                                        [checkInCalendarItem setWholeDay:NO];

                                        [checkInCalendarItem setStartTime:[dateFormatter dateFromString:checkinTimeString]];

                                        [checkInCalendarItem setEndTime:[checkInCalendarItem startTime]];

                                        [checkInCalendarItem setSubject:@"Check In"];

                                        [[self calendarItems] addObject:checkInCalendarItem];

                                        checkInCalendarItem = nil;

                                    }

                                }

                                else

                                {

                                    [mydelegate myErrorLogFromClass:self andMethod:@"getRoster:Online:" withFreeText:[NSString stringWithFormat:@"STD ikke 4 lang. Line: %@", line] andListofIvars:nil];

                                }

                            }

                            else

                            {

                                [mydelegate myErrorLogFromClass:self andMethod:@"getRoster:Online:" withFreeText:[NSString stringWithFormat:@"STD er nil. Line: %@", line] andListofIvars:nil];

                            }

                            if (param8sta != nil)

                            {

                                if ([param8sta length] == 4) {

                                    NSString *dateAndTimeString = [param1date stringByAppendingString:param8sta];

                                    NSLocale * enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

                                    [dateFormatter setLocale:enUSPOSIXLocale];

                                    NSTimeZone *thisTimeZone2;

                                    if (param9to != nil)

                                    {

                                        thisTimeZone2 = [self timeZoneForAirport:param9to];

                                    }

                                    else if (param6from != nil) //Kan skje ved stby

                                    {

                                        thisTimeZone2 = [self timeZoneForAirport:param6from];

                                        NSLog(@"thisTimeZone2 satt til param6From for param9To er nil. Dato:%@", dateAndTimeString);

                                    }

                                    if (thisTimeZone2 == nil)

                                    {

                                        thisTimeZone2 = [NSTimeZone timeZoneWithName:@"Europe/Oslo"];

                                        NSLog(@"thisTimeZone2 satt til OSL. param9to er nil.");

                                    }

                                    [dateFormatter setTimeZone:thisTimeZone2];

                                    [dateFormatter setDateFormat:@"ddMMMyyHHmm"];

                                    NSDate *tempoDate = [dateFormatter dateFromString:dateAndTimeString];

                                    if([tempoDate compare:newCalendarItem.startTime] == NSOrderedAscending)

                                    {

                                        tempoDate = [NSDate dateWithTimeInterval:86400 sinceDate:tempoDate];

                                    }

                                    [newCalendarItem setEndTime:tempoDate];

                                }

                                else

                                {

                                    [mydelegate myErrorLogFromClass:self andMethod:@"getroster:online:" withFreeText:[NSString stringWithFormat:@"STA-felt ikke 4 lang. Line: %@",line] andListofIvars:nil];

                                }

                            }

                            else

                            {

                                    [mydelegate myErrorLogFromClass:self andMethod:@"getroster:online:" withFreeText:[NSString stringWithFormat:@"STA-felt er nil. Line: %@",line] andListofIvars:nil];

                            }

                        }

                        if (param3activity != nil)

                        {

                            if ([param3activity rangeOfString:@"GO"].location == NSNotFound &&

                                [param3activity rangeOfString:@"VO"].location == NSNotFound)

                            {

                                if ([self itemOkAccordingToPreferences:newCalendarItem])

                                {

                                    [[self calendarItems] addObject:newCalendarItem];

                                }                                   

                            }

                        }

                        param2duty = nil;

                        param3activity = nil;

                        param6from = nil;

                        param7std = nil;

                        param8sta =nil;

                        param9to = nil;

                        param5checkIn = nil;

                    }               

                } //End of if ([partOfLine length]>0)               

            }

        }

    } // End of for (NSString* line in lines)

    NSLog(@"Ferdig med å tygge dataene. Funnet %d calendarItems", [[self calendarItems] count]);

    NSArray *tempoArray2 = [[NSArray alloc]initWithArray:[self calendarItems]];

    NSLog(@"tempoArray2 fyllt med calendarItems. tempoArray2.count:%d", tempoArray2.count);

    return tempoArray2;

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
If this really is all the code, then I can think of some scenarios where this would crash. First, if you have multiple threads, it's very likely you're crashing on another thread.

If this is the only thread, then I would suspect somewhere in "line 5" you create data that is incorrectly memory managed. In ARC, this requires either that you're calling out to non-ARC code, or you're doing __bridge casts at some point, or you incorrectly manage malloc memory (perhaps in the dealloc of something created in "line 5").

At line 2, tempoArray may be immediately deallocated if it is not used after line 2. If it is used after line 2, then it is almost inconceivable that it would be released too early unless you've done something really strange (like having the return type of getRoster... be __unsafe_unretained NSArray*).

Another possibility is if you're smashing your own stack. This can happen if you allocate C arrays or strings on your stack and then write off the end of them. This would be most likely in "line 5."

The fact that you see this in the field suggests that it may be optimization related. Make sure to test with Release mode.

Your naming conventions are incorrect, which can create problems in ARC code, though I doubt this is the actual problem. The leading get in getRoster:online: should be removed. A leading get indicates that the method returns its result by reference. That said, I don't believe ARC applies any special memory management in this case, so I don't think it's causing your crash. See the KVC guide for full details on correct naming so that ARC knows what you're doing.

The fact that symbolication isn't working well suggests that you might be looking at a different dSYM file than the code you shipped. Make sure you always save the dSYM and binary of the package you send to app store. Xcode uses spotlight to find a matching dSYM.
...