简体   繁体   中英

crash at [NSFileManager removeItemAtPath:error:]

I am trying to remove a file from path using following code. But my application is crashing while removing file from the path.

- (void)saveEditedSavedFile:(NSString*)editedfile As:(NSString*)originalFile
{
    [originalFile retain];
    NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docsDir = [dirPaths objectAtIndex:0]; 

    NSError* error = nil;

    NSString *editedFilePath=[docsDir stringByAppendingPathComponent:editedfile];

    NSFileManager* fileMngr = [NSFileManager defaultManager];

    if([fileMngr fileExistsAtPath:originalFile])
    {
nslog(@"%@", originalFile); // nslog always prints the correct path even if it crashes..
        [fileMngr removeItemAtPath:originalFile error:NULL];
    }

    if ([fileMngr moveItemAtPath:editedFilePath toPath:originalFile error:&error] != YES)
    {
        UIAlertView* alertView1 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to Save File. Please Choose a Diffrent name." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView1 show];
        [alertView1 release];

    }

}

Here i am trying to replace an already existing file with a new file.(Moving a file operation) but it crashes at [fileMngr removeItemAtPath:originalFile error:NULL];

I am getting a call stack like this ..

#0  0x99f1dc5a in __kill ()
#1  0x99f1dc4c in kill$UNIX2003 ()
#2  0x99fb05a5 in raise ()
#3  0x99fc66e4 in abort ()
#4  0x99fb4e78 in szone_error ()
#5  0x99fb4fb3 in free_list_checksum_botch ()
#6  0x99ec7a88 in small_free_list_remove_ptr ()
#7  0x99ec45cc in szone_free_definite_size ()
#8  0x99ec35e8 in free ()
#9  0x99ee8adb in fts_close$INODE64 ()
#10 0x99f31b57 in __removefile_tree_walker ()
#11 0x99f31999 in removefile ()
#12 0x0006ed01 in -[NSFilesystemItemRemoveOperation main] ()
#13 0x0005cbd2 in -[__NSOperationInternal start] ()
#14 0x0006eaa2 in -[NSFileManager removeItemAtPath:error:] ()

can anyone let me know why my application is crashing? i checked that that path is coming as correct there.

Best guess is that originalFile has been over released and at some point during the execution of -removeItemAtPath:error: its memory has been reallocated. Try running it with the zombies profiling tool.

The stack trace looks like there's an implementation error in NSFileManager .

The functions that do the dirty work of deleting (nested) files ( remove file and __removefile_tree_walker ) use BSD's "fts" facility (see man fts ) to walk the directory structure. In the latter function an FTS handle gets closed that is not open. This is definitely a bug in the implementation. You should file a radar .

How that can happen is a matter of guessing. I'd look in the direction of concurrent operations on the filesystem (parallel deletion on the same item, moving part of the hierarchy while it gets deleted, ...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM