0

I have a project written for iOS 4.x. Recently i update it to iOS5 using XCode 4.3.2. It's strange that the app stop everytime with double free error when using Apple LLVM compiler. After i change back to LLVM GCC, it works fine. Is there any difference between this two? The code is shown below:

- (NSArray *)readCourselist {

    NSString *path = [[self currentUserPathString] stringByAppendingPathComponent:kUserCourseListFilename];

    return [NSArray arrayWithContentsOfFile:path];

}

- (NSArray *)getCourselist {

    NSArray *courseRawArray = [self readCourselist];

    for (NSDictionary *courseDic in courseRawArray) {

        CourseModel *courseModel = [[CourseModel alloc] init];

        courseModel.courseID = [[courseDic objectForKey:kKeyID] intValue];

        courseModel.courseNameString = [courseDic objectForKey:kKeyTitle];

        NSArray *lectureArray = [courseDic objectForKey:kKeyLecture];

        for (NSDictionary *lectureDic in lectureArray) {

            LectureModel *lectureModel = [[LectureModel alloc] init];

            NSString *startString = [lectureDic objectForKey:kKeyStart];


            if ([startString isEqualToString:@"8:00"]) {

                lectureModel.lectureNumber = 1;

            }

            else if ([startString isEqualToString:@"9:50"]) {

                lectureModel.lectureNumber = 2;

            }



            lectureModel.location = [lectureDic objectForKey:kKeyWhere];  //@property of location is retain

            [courseModel.lectureArray addObject:lectureModel];

            [lectureModel release];

        }

        [courseArray addObject:courseModel];

        [courseModel release];

    }

}

With more tracing i found out that it's

lectureModel.location = [lectureDic objectForKey:kKeyWhere];

that really matters. In my LectureModel, location is declared as follow

@property (nonatomic, retain) NSString *location;

@synthesize location;

- (id)init {
    location = NSLocalizedString(@"未知", nil);
}

Delete NSLocalizedString and everything works all right. Why?

xi.lin
  • 3,326
  • 2
  • 31
  • 57

1 Answers1

0

Generally working with NSDictionary you want to use valueForKey: instead of objectForKey:, but I don't think that's the problem here. If you flip it back to LLVM, and run Instruments with "Zombies", it should point you to exactly where each free (well, release) is occurring.

Scott Corscadden
  • 2,831
  • 1
  • 25
  • 43
  • 2
    I disagree with the advice to use `-valueForKey:`. `-objectForKey:` is the native interface of `NSDictionary`. `-valueForKey:` is a KVC addition. It has both additional functionality that one may not want as well as additional restrictions, such as the key having to be a string. – Ken Thomases Apr 14 '12 at 14:44
  • Ken, I stand corrected. You are absolutely correct there. I would have down voted my original answer, but one cannot vote for one's own answers. I do stand by the Zombie instruction though. – Scott Corscadden Apr 15 '12 at 11:25
  • Oh, I definitely agree with the suggestion to use the Zombies instrument to track memory management errors. :) – Ken Thomases Apr 15 '12 at 15:02
  • I set NSZombieEnabled in scheme but it didn't show me the error release. Actually it didn't show the double free error in ipad simulator. It only happen in my ipad 2 with 5.0.1... – xi.lin Apr 17 '12 at 14:18
  • Run the Zombie tool, in Instruments, directly with the device (not the simulator). – Scott Corscadden Apr 17 '12 at 17:51