0

I have an old App which my company still provides legacy support for.

There's a strange error that's occurring for some of our clients but not on our end. It apparently happens when the user picks some image from ImagePickerController.

Exception

The exception says:

Fatal Exception: NSInvalidArgumentException

-[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

Stack trace

The stack trace is:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2017b7ea0 __exceptionPreprocess
1  libobjc.A.dylib                0x200989a40 objc_exception_throw
2  CoreFoundation                 0x20172f470 _CFArgv
3  CoreFoundation                 0x2016a85f4 -[__NSPlaceholderArray initWithObjects:count:]
4  CoreFoundation                 0x2017b3600 __createArray
5  CoreFoundation                 0x2016b0584 +[NSArray arrayWithObject:]
6  MyAppName             0x102191378 -[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] (PSImageViewController.m:221)
7  UIKitCore                      0x22e30e30c -[UIImagePickerController _imagePickerDidCompleteWithInfo:]
8  PhotoLibrary                   0x214add528 (Missing)
9  CameraUI                       0x21f38c728 -[CAMImagePickerCameraViewController _handleCapturedImagePickerPhotoWithCropOverlayOutput:]
10 AssetsLibraryServices          0x20f173814 __pl_dispatch_sync_block_invoke
11 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
12 libdispatch.dylib              0x20119f6b0 _dispatch_async_and_wait_invoke
13 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
14 libdispatch.dylib              0x20119e9b4 _dispatch_main_queue_callback_4CF$VARIANT$mp
15 CoreFoundation                 0x201747dd0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                 0x201742c98 __CFRunLoopRun
17 CoreFoundation                 0x2017421cc CFRunLoopRunSpecific
18 GraphicsServices               0x2039b9584 GSEventRunModal
19 UIKitCore                      0x22e985054 UIApplicationMain
20 MyAppName             0x1020d60fc main (main.m:17)
21 libdyld.dylib                  0x201202bb4 start

Code

My code for ImagePickerController's delegate is:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if([info objectForKey:UIImagePickerControllerEditedImage])
    {
        self.image = [info valueForKey:UIImagePickerControllerEditedImage];
    }
    else
    {
        self.image = [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    [self.imgProperty setImage:self.image];
     [[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];
    [self dismissViewControllerAnimated:YES completion:nil];

}

As you can see, I am not actually doing anything here that might cause that exception. The code which opens the imagePicker is:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    CLS_LOG(@"Clicked Index %d",buttonIndex);

    if (actionSheet.tag == 1)
    {
        if (buttonIndex != actionSheet.cancelButtonIndex)
        {
            if (buttonIndex == actionSheet.firstOtherButtonIndex)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypeCamera];

            }
            else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; return;
            }

        }
    }
}

The specific line in the trace PhotoLibrary 0x214add528 (Missing) is very curious. I think somehow the user doesn't have a PhotoLibrary or that somehow a wrong media source is chosen for Imagepicker. Or is it

[[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];

which is causing the issue with self.propertyObj.propertyToAppointment somehow being null? I have checked, it can't be null. Or there would be more crashes on this screen. It is always valid in my test-runs.

How can I fix this crash?

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • Which line is line 221 in your `didFinishPickingMediaWithInfo` method? – rmaddy Feb 25 '19 at 16:27
  • It's probably from `[NSArray arrayWithObject:self.propertyObj.propertyToAppointment]`. This means that `self.propertyObj.propertyToAppointment` is `nil`. – rmaddy Feb 25 '19 at 16:28
  • @rmaddy `[[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];` is line 221 – NSNoob Feb 25 '19 at 16:28
  • Since frame 5 mentions `+[NSArray arrayWithObject:]` I would say your issue is with the `PSDataPersistenceManager` line. Perhaps the other places that call for that value are more tolerant of nil. – theMikeSwan Feb 25 '19 at 16:29
  • FYI - use modern syntax. Replace `[NSArray arrayWithObject: something]` with `@[ something ]`. And replace `[NSNumber numberWithBool:YES]` with `@YES`. – rmaddy Feb 25 '19 at 16:29
  • I would just add a check to make sure you can get a value out of `self.propertyObj.propertyToAppointment` before trying to create an array with it. If you have an analytics engine in the app maybe add a report whenever it comes up nil and see if you can find the root cause. – theMikeSwan Feb 25 '19 at 16:32
  • @theMikeSwan Aye that would solve this specific problem but then cause tons of other problems given the core assumption here was that if there's no valid appointment, user can't go to that screen. Apparently, the user can. I'll have to look for whatever is making the appointment null, thanks for your help – NSNoob Feb 25 '19 at 16:35

1 Answers1

2

The error is from line 221. You create an array with:

[NSArray arrayWithObject:self.propertyObj.propertyToAppointment]

The error is telling you that self.propertyObj.propertyToAppointment is nil.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Hmm I suspected that line might be the culprit given the stack and it being the only thing that's initialising an array but Idk how come it's null. User can't actually go to that screen without a valid `self.propertyObj.propertyToAppointment` link. Guess I'll just have to look for whatever is making it null. Thanks for the help. – NSNoob Feb 25 '19 at 16:33
  • 1
    Keep in mind that either `self.propertyObj` is nil or `self.propertyObj.propertyToAppointment` is nil. – rmaddy Feb 25 '19 at 16:34