Note that there are some explanatory texts on larger screens.

plurals
  1. POrelease of previously deallocated object issue
    text
    copied!<p>I have a function which use for read one single line from a csv file. But I got a release of previously deallocated object error, or sometimes the it is "double free" error.</p> <p>I try to track down which object causes this error base on the error memory address, but I failed to do this.</p> <p>Here's the code:</p> <pre><code> @interface CSVParser : NSObject { NSString *fileName; NSString *filePath; NSString *tempFileName; NSString *tempFilePath; //ReadLine control BOOL isFirstTimeLoadFile; NSString *remainContent; } @property(nonatomic,retain) NSString *fileName; @property(nonatomic,retain) NSString *filePath; @property(nonatomic,retain) NSString *tempFileName; @property(nonatomic,retain) NSString *tempFilePath; @property(nonatomic,retain) NSString *remainContent; -(id)initWithFileName:(NSString*)filename; -(BOOL)checkAndCopyFile:(NSString *)filename; -(BOOL)checkAndDeleteTempFile; -(NSString*)readLine; -(NSArray*)breakLine:(NSString*)line; @end @implementation CSVParser @synthesize fileName; @synthesize filePath; @synthesize tempFileName; @synthesize tempFilePath; @synthesize remainContent; -(id)initWithFileName:(NSString *)filename{ //ReadLine control isFirstTimeLoadFile = TRUE; self.fileName = filename; self.tempFileName = [[NSString alloc] initWithFormat:@"temp_%@",fileName]; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPaths objectAtIndex:0]; self.filePath = [documentDir stringByAppendingPathComponent:fileName]; self.tempFilePath = [documentDir stringByAppendingPathComponent:tempFileName]; if ([self checkAndCopyFile:fileName]) { return self; }else { return @"Init Failure"; } } -(BOOL)checkAndCopyFile:(NSString *)filename{ BOOL isFileExist; NSError *error = nil; NSFileManager *fileManger = [NSFileManager defaultManager]; isFileExist = [fileManger fileExistsAtPath:filePath]; if (isFileExist) { //Create a temp file for reading the line. [fileManger copyItemAtPath:filePath toPath:tempFilePath error:&amp;error]; return TRUE; }else { return FALSE; } } -(NSString*)readLine{ NSError *error = nil; //Read the csv file and save it as a string NSString *tempFirstLine = [[[NSString alloc] init] autorelease]; NSString *stringFromFileAtPath = [[NSString alloc] init]; if (isFirstTimeLoadFile) { NSLog(@"Into First Time"); stringFromFileAtPath = [NSString stringWithContentsOfFile:tempFilePath encoding:NSUTF8StringEncoding error:&amp;error]; isFirstTimeLoadFile = FALSE; }else { NSLog(@"Not First Time"); NSLog(@"Not First Time count:%d",[remainContent retainCount]); stringFromFileAtPath = remainContent; remainContent = nil; } if ([stringFromFileAtPath isEqualToString:@""]) { [stringFromFileAtPath release]; return @"EOF"; } //Get the first line's range NSRange firstLineRange = [stringFromFileAtPath rangeOfString:@"\n"]; //Create a new range for deletion. This range's lenght is bigger than the first line by 1.(Including the \n) NSRange firstLineChangeLineIncludedRange; if (stringFromFileAtPath.length &gt; 0 &amp;&amp; firstLineRange.length == 0) { //This is the final line. firstLineRange.length = stringFromFileAtPath.length; firstLineRange.location = 0; firstLineChangeLineIncludedRange = firstLineRange; }else { firstLineRange.length = firstLineRange.location; firstLineRange.location = 0; firstLineChangeLineIncludedRange.location = firstLineRange.location; firstLineChangeLineIncludedRange.length = firstLineRange.length + 1; } //Get the first line's content tempFirstLine = [stringFromFileAtPath substringWithRange:firstLineRange]; remainContent = [stringFromFileAtPath stringByReplacingCharactersInRange:firstLineChangeLineIncludedRange withString:@""]; [stringFromFileAtPath release]; error = nil; return tempFirstLine; } </code></pre> <p>And the following code shows how I use the class above:</p> <pre><code>CSVParser *csvParser = [[CSVParser alloc] initWithFileName:@"test.csv"]; BOOL isFinalLine = FALSE; while (!isFinalLine) { NSString *line = [[NSString alloc] init]; line = [csvParser readLine]; if ([line isEqualToString:@"EOF"]) { isFinalLine = TRUE; } NSLog(@"%@",line); [line release]; } [csvParser release]; </code></pre> <p>If I run the code, and finish the csv parsing, the App's main function will give me the double free error when it try to free the autorelease pool."<em>*</em> __NSAutoreleaseFreedObject(): release of previously deallocated object (0x6a26050) ignored"</p> <p>NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil);</p> <p>Could someone help me solve this issue? Thank you! [pool release];</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload