EXC_BAD_ACCESS on NSSearchPathForDirectoriesInDomains

佐手、 提交于 2019-12-24 06:38:31

问题


I am working on a new release for an iOS application that was developed by another company but is now my responsibility. I have very little experience with objective C in general and iOS development in particular, but I'll try to convey my problem as best I can.

I have the following method that I neither made nor changed over the course of my improvements:

- (Boolean)ShouldWeDownloadImage:(NSString*)p_ImageName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], p_ImageName];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (!([fileManager fileExistsAtPath:filePath]))
        return true;

    NSDate *creationDate = [[fileManager attributesOfItemAtPath:filePath error:nil]objectForKey:NSFileCreationDate];
    NSDate *currentDate = [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:NSMonthCalendarUnit fromDate:creationDate toDate:currentDate options:0];

    if ([dateComponents month] > MaxAgeOfDownloadedImage)
        return true;

    return false;
}

This particular method gets called a lot. And I do mean a lot; dozens (or even hundreds) of times in short succession in fact. So does the method that calls it, and the method that calls that, for that matter. My issue is that whenever I start my app, it starts downloading any required images right away, and after a few seconds the app will just crash when I run it on an actual device. The simulator however is fine and runs the app without any issue. Of note is that the simulator runs iOS7 and the physical device I test on runs iOS6. I have no other devices I can test on.

When the app crashes, I get an EXC_BAD_ACCESS message on the first line of this method. Suspecting it might be because of the many subsequent calls to this method and the effects this might have on the memory usage, I moved the definition of the paths array to its another class where it is stored permanently and reused by passing it to this method as a parameter. After I did that, the app no longer crashed on the first line, but on the definition of the NSCalendar. I figured that the app could do without the timestamp check and commented that bit of code out, but once I did, the new first line of the method (the definition of filePath) made the app crash instead.

The best part: if I take out all this code and just return true from this method, the app works fine, which leads me to believe that whatever is wrong, it's wrong in this method and only in this method.

Of note: the project was converted to ARC to try and help combat this problem, but to no avail. I also tried enabling zombie objects to give me more information but I can't find any useful information when debugging even with that turned on.

Does anyone know what this method might be doing wrong and how to fix it? Simply returning true will make the application usable but it would not be an acceptable solution as this would mean it will download a lot of unnecessary data over my users' expensive data plans.

来源:https://stackoverflow.com/questions/20246270/exc-bad-access-on-nssearchpathfordirectoriesindomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!