尝试呈现UIActivityViewController时崩溃

时间:2022-05-12 16:04:07

I see a crash in Crashlytics than happens to my users sometimes. The crash happens when presenting UIActivityViewController in the last line of the following code:

我发现Crashlytics的崩溃比我的用户有时会发生。在以下代码的最后一行中显示UIActivityViewController时发生崩溃:

NSData* snapShot = ... ;

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:activityTextsProvider, snapShot ,nil] applicationActivities:[NSArray arrayWithObjects:customActivityA, customActivityB, customActivityC, nullptr]];

activityViewController.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeMail, UIActivityTypeCopyToPasteboard, nil];
activityViewController.popoverPresentationController.sourceView = self.myButton;
activityViewController.popoverPresentationController.sourceRect = self.myButton.bounds;

activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError)
{
    ...
};

[self presentViewController:activityViewController animated:YES completion:nil];

I perform this in the main thread and unable to reproduce this crash locally. What could be the reason of this crash?

我在主线程中执行此操作,无法在本地重现此崩溃。这次崩溃的原因是什么?


Edit: I changed nullptr to nil and the issue still happened. I managed to reproduce the issue: the crash happens only if before opening the activity controller i showed a UIMenuController. When creating UIActivityViewController it is not nil, but when presenting the controller i see the crash in the presentViewController line and the activity controller there is shown as nil

编辑:我将nullptr更改为nil,问题仍然存在。我设法重现了这个问题:崩溃只发生在打开活动控制器之前我才显示UIMenuController。在创建UIActivityViewController时,它不是零,但在呈现控制器时,我看到presentViewController行和活动控制器中的崩溃显示为nil

1 个解决方案

#1


József addressed the use of nullptr in comments, and Fogh is spot-on that the actual crash log is important (please edit your question and post the full crash log), but I'd like to point out something else.

József在评论中解决了nullptr的使用问题,并且Fogh认为实际的崩溃日志很重要(请编辑你的问题并发布完整的崩溃日志),但我想指出其他的东西。

You're assuming your call to initialize activityViewController is succeeding. You should code defensively (by assuming everything that can fail probably will fail and testing for this at runtime). Wrap the rest of the configuration and presentation inside an if (activityViewController != nil) {} condition (you should probably have an else with proper error handling/reporting too) so you're properly detecting an all-out initialization failure for multiple reasons (like a misplaced nib, missing resource, etc.).

您假设您初始化activityViewController的调用正在成功。您应该进行防御性编码(通过假设可能失败的所有内容可能会失败并在运行时对此进行测试)。将其余的配置和演示文稿包含在if(activityViewController!= nil){}条件中(您可能还有其他正确的错误处理/报告),因此您可以出于多种原因正确检测全部初始化失败(就像一个错位的笔尖,缺少资源等)。

In your case, I think it's likely the initialization is failing because your class is doing something with a faulty array, as József's nullptr catch suggests. Perhaps you're using one or more pre-C++11 c libraries / compiling with a non-C11/gnu11 "C Language Dialect" build setting and nullptr is not equivalent to nil, leading to strange results in a supposed-to-be-nil-terminated array?

在你的情况下,我认为初始化可能是失败的,因为你的类正在使用错误的数组做事,正如József的nullptr catch所暗示的那样。也许您正在使用一个或多个pre-C ++ 11 c库/使用非C11 / gnu11“C语言方言”构建设置进行编译,并且nullptr不等于nil,导致假设的奇怪结果be-nil-terminated数组?

Note: If that turns out to be the case, I'll happily take an upvote but would rather József post his comment as an answer so you can give him proper credit. (Feel free to edit this request out of my answer if/when that happens.)

注意:如果事实证明是这样的话,我会高兴地接受一个upvote,但我宁愿József发表他的评论作为答案,所以你可以给他适当的信誉。 (如果发生这种情况,请随时根据我的回答编辑此请求。)

#1


József addressed the use of nullptr in comments, and Fogh is spot-on that the actual crash log is important (please edit your question and post the full crash log), but I'd like to point out something else.

József在评论中解决了nullptr的使用问题,并且Fogh认为实际的崩溃日志很重要(请编辑你的问题并发布完整的崩溃日志),但我想指出其他的东西。

You're assuming your call to initialize activityViewController is succeeding. You should code defensively (by assuming everything that can fail probably will fail and testing for this at runtime). Wrap the rest of the configuration and presentation inside an if (activityViewController != nil) {} condition (you should probably have an else with proper error handling/reporting too) so you're properly detecting an all-out initialization failure for multiple reasons (like a misplaced nib, missing resource, etc.).

您假设您初始化activityViewController的调用正在成功。您应该进行防御性编码(通过假设可能失败的所有内容可能会失败并在运行时对此进行测试)。将其余的配置和演示文稿包含在if(activityViewController!= nil){}条件中(您可能还有其他正确的错误处理/报告),因此您可以出于多种原因正确检测全部初始化失败(就像一个错位的笔尖,缺少资源等)。

In your case, I think it's likely the initialization is failing because your class is doing something with a faulty array, as József's nullptr catch suggests. Perhaps you're using one or more pre-C++11 c libraries / compiling with a non-C11/gnu11 "C Language Dialect" build setting and nullptr is not equivalent to nil, leading to strange results in a supposed-to-be-nil-terminated array?

在你的情况下,我认为初始化可能是失败的,因为你的类正在使用错误的数组做事,正如József的nullptr catch所暗示的那样。也许您正在使用一个或多个pre-C ++ 11 c库/使用非C11 / gnu11“C语言方言”构建设置进行编译,并且nullptr不等于nil,导致假设的奇怪结果be-nil-terminated数组?

Note: If that turns out to be the case, I'll happily take an upvote but would rather József post his comment as an answer so you can give him proper credit. (Feel free to edit this request out of my answer if/when that happens.)

注意:如果事实证明是这样的话,我会高兴地接受一个upvote,但我宁愿József发表他的评论作为答案,所以你可以给他适当的信誉。 (如果发生这种情况,请随时根据我的回答编辑此请求。)