0

Here is the code below. It downloads a thumbnail image and then tries to create an image based on the thumbnail file path. But it gives me EXC_BAD_ACCESS error at method call "imageWithContentsOfFile". While EXC_BAD_ACCESS addresses the code trying to access an object that has been released most likely I don't know which object it could be. Any help'd be appreciated!

NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
            if([[NSFileManager defaultManager] fileExistsAtPath:operation.destinationPath ]){

               NSString *key = [[MEURLCacheKeyRegister sharedRegister] cacheKeyForURL:operation.fileUrl];

               UIImage *image = [UIImage imageWithContentsOfFile:operation.destinationPath];
            }else{
                DDLogDebug(@"Thumbnail file doesn't exist at %@", operation.destinationPath);
            }
        }

    }];

AFDownloadRequestOperation *requestOperation = [FileServerDownloadUtils downloadOperationForURL:operation.fileUrl
                                                                                      destinationPath:operation.destinationPath
                                                                                           completion:completionOperation];
[self.fileSyncQueue addOperation:requestOperation];
JohnnL
  • 111
  • 11
  • Which line it crashes and what are you doing with `image` variable after loading in completion block? – Abhinav Oct 28 '15 at 06:10
  • Hi Abhinav, it crashed at line UIImage *image = [UIImage imageWithContentsOfFile:operation.destinationPath]; – JohnnL Oct 28 '15 at 06:13
  • operation is a custom NSOperation object BTW. – JohnnL Oct 28 '15 at 06:14
  • What are you doing with that `image` and also can you print your `destinationPath` and show here. I doubt if it contains image file name in the end. – Abhinav Oct 28 '15 at 06:17
  • I didn't post the usage of image object as it's not related to the problem I am having. The image file name is "beacon_preview.jpg.prv.jpg" and as you can see the file does exist. The same code works in other places. – JohnnL Oct 28 '15 at 06:28

1 Answers1

0

EXC_BAD_ACCESS indicates that object has been released while its being accessed.

If I were you, I would try following things:

  1. Save the file with .jpg and not .jpg.prv.jpg extension.
  2. Try using initWithContentsOfFile instead of imageWithContentsOfFile as imageWithContentsOfFile autoreleases image which in rare edge cases creates crashes like this.
  3. When passing code to a block, access object properties by making weak reference to self. Something like this: __weak MyController *weakSelf = self. Then use weakSelf to access properties inside the block.

These are just few clues which may help you digging it further. You may use NSZombie and other profiling tools to nail it down.

Abhinav
  • 37,684
  • 43
  • 191
  • 309