0

I have been testing an IOS app on multiple platforms and have observed some weird behaviours while testing on the iPhone 4S. The app crashes after performing a task that works on both the iPhone 5 and iPhone 3S. When the crash happens there is nothing displayed on the debugger, absolutely no crash log or memory warnings appear in Xcode. I isolated the block of code, and the specific line responsible for the crash. I still don't understand why it is happening or how to find a solution. The code is part of a larger method that downloads a number of images and displays them in UIImageViews. Here is the code:

//fill image tiles with images
for (int i = 0; i < [objects count]; ++i)
{
    PFObject *imageObject = [objects objectAtIndex:i];
    PFFile *imageFile = (PFFile *)[imageObject objectForKey:@"image"];
    UIImageView *imageHolder = (UIImageView *)[self.view viewWithTag:(100 + i)];

    imageHolder.image = [UIImage imageWithData:imageFile.getData];
}

The code loops through an object that has been loaded with image files from a server. The UIImageViews that each image is meant to be assigned to are tagged from 100 - 104, so they can be accessed with the loop index variable. The line UIImageView *imageHolder = (UIImageView *)[self.view viewWithTag:(100 + i)] extracts the UIImageViews from the main view. In the next line the imageHolder view is assigned an image, this is the line that causes the crash, and when commented out the view loads without fail. I haven't been able to determine why this is happening or if it is the imageFile or imageHolder view that is not being set up properly. Perhaps someone here can shed some light on the problem.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
ScottOBot
  • 839
  • 3
  • 16
  • 37
  • It could be allocating too much memory and the OS is terminating the app without warning. Happened to me recently, no exception, no memory warning, it just terminated. Separate that `imageFile.getData` call onto a separate line with a variable holding the `NSData`, then pass the data to `imageWithData`. That will help find out which of those two methods is causing a problem. – progrmr Aug 01 '13 at 23:34
  • Try adding a call to `logMemUsage()` in that loop, the code for that is [here](http://stackoverflow.com/a/2921725/1693173) and you can see how you free memory is doing as you go through the loop. – progrmr Aug 01 '13 at 23:38

1 Answers1

1

You should have some protection around imageFile.getData as it could return nil. You should preferable use the getData: method which returns an error that you can use if the data can't be accessed.


NSError *error = nil;
NSData *imageData = [imageFile getData:&error];

if (imageData != nil) {
    imageHolder.image = [UIImage imageWithData:imageData];
} else {
    NSLog(@"OMG!! %@", error);
}
Wain
  • 118,658
  • 15
  • 128
  • 151
  • What class is the `getData:` method from and how is it used? @Wain – ScottOBot Aug 01 '13 at 20:07
  • `PFFile`, you're already calling the `getData` method (by dot notation). https://www.parse.com/docs/ios/api/Classes/PFFile.html#//api/name/getData: – Wain Aug 01 '13 at 20:12
  • So you mean use `[imageFile getData]` instead of `imageFile.getData`? Sorry for making you spell it out, im relativly new to this. @Wain – ScottOBot Aug 01 '13 at 20:21
  • Also the phone actually has to restart sometimes, could it just be a bad test phone? @Wain – ScottOBot Aug 01 '13 at 20:27
  • It's possible that something about the phone could be causing downloads to fail... – Wain Aug 01 '13 at 20:29
  • Yeah I implemented your solution and it didn't seem to change anything why isn't anything being displayed in the debugger output does that just mean that no known exception is being thrown? @Wain – ScottOBot Aug 01 '13 at 20:34
  • Do you have an all exceptions breakpoint? Have you tried continuing past it? You should get something. Not getting anything could indicate recursion but that should hard crash sooner or later too. – Wain Aug 01 '13 at 20:36
  • I have been playing around with the IOS instruments installed with Xcode and it appears to be crashing after receiving a number of memory warnings. What could be causing this? I have a number of long-running operations in my main thread, if I was to put them in a sub thread would that reduce my apps memory footprint? @Wain – ScottOBot Aug 05 '13 at 18:50
  • Look at the live bytes, how big are they, what are they, when were they created, should they have been released. – Wain Aug 05 '13 at 20:08
  • Thanks man protecting the getData method and using the instruments allowed me to figure out that the problem was that a massive image was being loaded. This done multiple times in a loop, caused the memory to be used up fairly quickly and explained why the code used with other images never failed. Thanks for all the great help! @Wain – ScottOBot Aug 06 '13 at 19:38