简体   繁体   中英

Get EXEC_BAD_ACCESS when I get the NSFileModificationDate

I try to get the last modification date of a file:

NSFileManager *fm = [[NSFileManager alloc] init];
NSError *err;
NSDate *lastModif = [[fm attributesOfItemAtPath:filename error:&err] objectForKey:NSFileModificationDate];//filename is ok ;-)
if(err == nil) {
    [lastModif retain];
    //I can put a NSLog of lastModif here, it works !!
    NSTimeInterval lastModifDiff = [lastModif timeIntervalSinceNow];//crash here
}

I don't understand why the NSDate seems to be released, why the retain does not retain it.

Thank you if you have any idea...

You don't need to retain lastModif . I think you might be trying to treat lastModifDiff as an object of some sort when you do an NSLog with it or whatever you do with it afterwards. NSTimeInterval is a typedef to a double so you need to treat it as a double or [NSNumber numberWithDouble:lastModifDiff] if you want to use it like an object.

I'm having the same problem, but this post seemed germane:

NSDate : timeIntervalSinceNow crash

I'm writing a simple set of functions- startClock/endClock -using NSDate to determine FPS in my game loop. Except that timeIntervalSinceNow crashes, claiming that my earlier set NSDate object doesn't exist.

I know for a fact that the NSDate object has a retain count of 1 when I call startClock, but my theory is that NSDate instances are internally rigged to auto-release when they get bored and aren't feeling useful.

Using retain/release to assume ownership of these flighty and ephemeral NSDate objects worked for me.

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