iOS 仅在不通过 XCode 运行时崩溃。信心?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9931439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:49:48  来源:igfitidea点击:

iOS crash only when NOT running via XCode. Concidence?

iosxcodedebuggingcrash

提问by msk

My app was crashing only when not running using XCode debugger. It was hard to track because I can't debug but I finally figured it out. It was because of calling release on some object not owned by me. Before I corrected it I searched and found 2 related questions here (links below)

我的应用程序仅在不使用 XCode 调试器运行时崩溃。很难跟踪,因为我无法调试,但我终于弄明白了。这是因为在某个不属于我的对象上调用了 release。在我更正之前,我在这里搜索并找到了 2 个相关问题(链接如下)

iOS App Crashes when running by itself on device, does not crash when running through Xcode using debugger, or in simulator

iOS 应用程序在设备上自行运行时崩溃,使用调试器或在模拟器中通过 Xcode 运行时不会崩溃

iPhone crash only when device not connected to xcode, how to understand the crash log?

iPhone 只有在设备未连接到 xcode 时才会崩溃,如何理解崩溃日志?

None of the above question has answered why no crash when running via debugger.So my question is why it happens ? I know reasons for debug/release specific crashes but this is crazy. Is it just by chance although it happened more than 10 times.

上述问题都没有回答为什么通过调试器运行时没有崩溃。所以我的问题是为什么会发生?我知道调试/发布特定崩溃的原因,但这很疯狂。虽然发生了10多次,但只是偶然吗?

采纳答案by DRVic

What you describe is not atypical of obscure memory-related bugs. You might also want to use debug-malloc at such times. Although that is not guaranteed to find everything. The reason (and it's been happening probably as long as there've been source-level debuggers) is that memory is laid out at least somewhat differently in debuggable code, and when running under the debugger. So the error results in a different piece of memory being (harmlessly) corrupted when under the debugger. When not under the debugger the location corrupted is actually something that your code cares about, and it crashes.

您所描述的不是与内存相关的晦涩错误的非典型。在这种情况下,您可能还想使用 debug-malloc。虽然不能保证找到所有内容。原因(并且可能只要有源代码级调试器就一直在发生)是可调试代码中的内存布局至少有些不同,并且在调试器下运行时。因此,在调试器下,错误会导致不同的内存(无害地)损坏。当不在调试器下时,损坏的位置实际上是您的代码关心的东西,它会崩溃。

The same could happen in reverse, but you'd never know - if it crashes when run debuggable, you'd find it before switching to running outside the debugging environment.

反过来也可能发生同样的情况,但您永远不会知道 - 如果它在运行可调试时崩溃,您会在切换到调试环境之外运行之前找到它。

回答by RyanJM

Reiterating @jyoung's answer since I didn't see it the first time I glanced through:

重申@jyoung 的回答,因为我第一次浏览时没有看到:

Try running with Zombie Objects turned off.

尝试在关闭 Zombie Objects 的情况下运行。

In debug mode if you have it turned on it is handling memory allocation differently. Try running it without.

在调试模式下,如果您打开它,它将以不同的方式处理内存分配。尝试在没有的情况下运行它。

Go to Edit Scheme... > Run > Diagnostics. Then make sure zombie objects is turned off:

转到编辑方案... > 运行 > 诊断。然后确保僵尸对象已关闭:

enter image description here

在此处输入图片说明

Then run through your code path again.

然后再次运行您的代码路径。

回答by Chris Schiffhauer

I had this issue when accessing SQLite databases from outside the [[NSBundle mainBundle] resourcePath] directory, which caused iCloud errors.

我在从 [[NSBundle mainBundle] resourcePath] 目录之外访问 SQLite 数据库时遇到了这个问题,这导致了 iCloud 错误。

I discovered the error only by installing a Console app onto my iPhone which logged the errors.

我只是通过在我的 iPhone 上安装一个控制台应用程序来发现错误,它记录了错误。

Once I accessed the databases from the correct directory, the errors disappeared and the application booted correctly.

一旦我从正确的目录访问数据库,错误就会消失并且应用程序正确启动。

回答by jyoung

I experienced this symptom when I made a NSString, sent a UTF8String from it to another object, and assigned it to a char pointer. Well, it turns out that I forgot to retain the original NSString, which wouldn't have mattered anyway, since I also failed to realize that the UTF8String method (which is presumably an object that gives access to the pointer itself) operates in the autorelease pool. That is, retaining the NSString itself did not fix the problem.

当我创建一个 NSString,从它发送一个 UTF8String 到另一个对象,并将它分配给一个字符指针时,我遇到了这个症状。好吧,事实证明我忘了保留原来的 NSString,这无论如何都无关紧要,因为我也没有意识到 UTF8String 方法(大概是一个可以访问指针本身的对象)在自动释放中运行水池。也就是说,保留 NSString 本身并没有解决问题。

I suppose this appeared to work just fine when attached under the debugger only because I had zombies enabled, so the pointer I had was still valid. I should see if this is the reason it worked; if so, this is a good reason to test with and without NSZombie enabled.

我想这在调试器下连接时似乎工作得很好,只是因为我启用了僵尸,所以我拥有的指针仍然有效。我应该看看这是否是它起作用的原因;如果是这样,这是在启用和不启用 NSZombie 的情况下进行测试的一个很好的理由。

At any rate, this was probably poor design to begin with, and a pretty obvious newbie memory management mistake once I found it. Luckily the console in the Organizer window gave me some hints on where to start looking, and debugging ultimately showed me where my pointer's value was changing. Hope this helps anyone who finds the way here.

无论如何,这可能是一个糟糕的设计开始,并且一旦我发现它是一个非常明显的新手内存管理错误。幸运的是,管理器窗口中的控制台给了我一些关于从哪里开始寻找的提示,调试最终向我展示了我的指针值发生变化的地方。希望这可以帮助任何在这里找到方法的人。

回答by DVigg

I was having this problem as well and was fortunate to figure out the cause quickly, hopefully by posting here I can save someone else some wasted time. To clarify, my app would run with no issues when launched directly from XCode, but would crash immediately when launched manually on the iPad.

我也遇到了这个问题,很幸运能够快速找出原因,希望通过在这里发布我可以节省其他人一些浪费的时间。澄清一下,我的应用程序在直接从 XCode 启动时不会出现问题,但在 iPad 上手动启动时会立即崩溃。

The app in question is written in Obj-C but relies on some 3rd party code written in Swift. The Swift code is included in the app as an embedded framework. I had to set "Embedded Content Contains Swift Code" to Yes in the Build Settings for the app (under Build Options), then the problem went away.

有问题的应用程序是用 Obj-C 编写的,但依赖于一些用 Swift 编写的第 3 方代码。Swift 代码作为嵌入式框架包含在应用程序中。我必须在应用程序的构建设置中(在构建选项下)将“嵌入式内容包含 Swift 代码”设置为是,然后问题就消失了。