如何调试EXC_BAD_ACCESS错误?

时间:2023-01-22 17:42:26

I received an error

我收到了一个错误

EXC_BAD_ACCESS code=2 at0xb0987654

= 2 at0xb0987654 EXC_BAD_ACCESS代码

I am wondering how to print out the value at 0xb0987654?

我想知道如何在0xb0987654打印值?

3 个解决方案

#1


30  

To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects.

要调试EXC_BAD_ACCESS,通常可以通过启用僵尸对象来找到悬浮指针的位置。

Xcode

Xcode

Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option.

选择“编辑模式”,然后在“运行”部分选择“诊断”选项卡,然后单击“僵尸对象”选项。

AppCode

本地

Choose edit target, and add the following environment variable:

选择编辑目标,添加以下环境变量:

NSZombieEnabled=YES

Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.

EXC_BAD_ACCESS的另一个原因可能是无限递归,这可以通过添加一些日志来找到。

Update for C++:

c++的更新:

To debug dangling pointers in C++ with the Clang compiler try using Address Sanitizer (ASAN) from Google.

要使用Clang编译器调试c++中的悬浮指针,请尝试使用谷歌中的地址清理器(ASAN)。

#2


2  

It looks like maybe you are trying to write onto a code page or something? EXC_BAD_ACCESS is described in /usr/include/mach/exception_types.h:

看起来好像你在写代码页什么的?EXC_BAD_ACCESS在/usr/include/mach/exception_types.h中描述。

#define EXC_BAD_ACCESS          1       /* Could not access memory */
            /* Code contains kern_return_t describing error. */
            /* Subcode contains bad memory address. */

And from kern_return.h:

从kern_return.h:

#define KERN_PROTECTION_FAILURE         2
            /* Specified memory is valid, but does not permit the
             * required forms of access.
             */

You can see WHERE that address is in your binary by doing:

通过以下操作,您可以看到地址在二进制文件中的位置:

(lldb) image lookup -va 0xb0987654

But what you really need to figure out is who is trying to write there. If the problem is simple this might tell you what's wrong, but as Jasper suggests, this is probably some use-after-free or other such problem, and the bad actor is long gone by the time you crash. guardmalloc can also sometimes catch this sort of error (you can enable this in Xcode in the Run scheme.)

但是你真正需要弄清楚的是谁在写。如果问题很简单,这可能会告诉你出了什么问题,但正如Jasper所说,这可能是一些不用后的问题,或者其他类似的问题,当你崩溃的时候,坏演员早就消失了。guardmalloc有时也会捕获这种错误(您可以在运行方案的Xcode中启用这种错误)。

#3


0  

Identify what you did that caused the crash. Did it crash while view of a particular view controller didLoad or in a delegate method or on a particular action. That will often help to find the object that is casuing the error.

确认你做了什么导致了事故。它是否在特定视图控制器didLoad或委托方法或特定操作的视图中崩溃?这通常有助于找到导致错误的对象。

  • Most of the time “NSZombies” can help to identify the dead object. You can enable NSZombies by editing your scheme Product -> Edit Scheme -> Diagnostics.
  • 大多数时候,“nszombie”可以帮助识别死去的物体。您可以通过编辑scheme产品->编辑方案->诊断来启用ns僵尸。
  • If you still don’t find the root cause then always go backwards from child view controller to parent view controller to see what object needs to be retained or what message needs to be passed properly.
  • 如果您仍然没有找到根本原因,那么总是从子视图控制器返回到父视图控制器,以查看需要保留什么对象或需要正确传递什么消息。
  • Look into Static Analyzer and Instruments for advanced debugging.
  • 查看静态分析仪和仪器以进行高级调试。

I hope this will help you.

我希望这能对你有所帮助。

Regards, Gison

问候,Gison

#1


30  

To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects.

要调试EXC_BAD_ACCESS,通常可以通过启用僵尸对象来找到悬浮指针的位置。

Xcode

Xcode

Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option.

选择“编辑模式”,然后在“运行”部分选择“诊断”选项卡,然后单击“僵尸对象”选项。

AppCode

本地

Choose edit target, and add the following environment variable:

选择编辑目标,添加以下环境变量:

NSZombieEnabled=YES

Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.

EXC_BAD_ACCESS的另一个原因可能是无限递归,这可以通过添加一些日志来找到。

Update for C++:

c++的更新:

To debug dangling pointers in C++ with the Clang compiler try using Address Sanitizer (ASAN) from Google.

要使用Clang编译器调试c++中的悬浮指针,请尝试使用谷歌中的地址清理器(ASAN)。

#2


2  

It looks like maybe you are trying to write onto a code page or something? EXC_BAD_ACCESS is described in /usr/include/mach/exception_types.h:

看起来好像你在写代码页什么的?EXC_BAD_ACCESS在/usr/include/mach/exception_types.h中描述。

#define EXC_BAD_ACCESS          1       /* Could not access memory */
            /* Code contains kern_return_t describing error. */
            /* Subcode contains bad memory address. */

And from kern_return.h:

从kern_return.h:

#define KERN_PROTECTION_FAILURE         2
            /* Specified memory is valid, but does not permit the
             * required forms of access.
             */

You can see WHERE that address is in your binary by doing:

通过以下操作,您可以看到地址在二进制文件中的位置:

(lldb) image lookup -va 0xb0987654

But what you really need to figure out is who is trying to write there. If the problem is simple this might tell you what's wrong, but as Jasper suggests, this is probably some use-after-free or other such problem, and the bad actor is long gone by the time you crash. guardmalloc can also sometimes catch this sort of error (you can enable this in Xcode in the Run scheme.)

但是你真正需要弄清楚的是谁在写。如果问题很简单,这可能会告诉你出了什么问题,但正如Jasper所说,这可能是一些不用后的问题,或者其他类似的问题,当你崩溃的时候,坏演员早就消失了。guardmalloc有时也会捕获这种错误(您可以在运行方案的Xcode中启用这种错误)。

#3


0  

Identify what you did that caused the crash. Did it crash while view of a particular view controller didLoad or in a delegate method or on a particular action. That will often help to find the object that is casuing the error.

确认你做了什么导致了事故。它是否在特定视图控制器didLoad或委托方法或特定操作的视图中崩溃?这通常有助于找到导致错误的对象。

  • Most of the time “NSZombies” can help to identify the dead object. You can enable NSZombies by editing your scheme Product -> Edit Scheme -> Diagnostics.
  • 大多数时候,“nszombie”可以帮助识别死去的物体。您可以通过编辑scheme产品->编辑方案->诊断来启用ns僵尸。
  • If you still don’t find the root cause then always go backwards from child view controller to parent view controller to see what object needs to be retained or what message needs to be passed properly.
  • 如果您仍然没有找到根本原因,那么总是从子视图控制器返回到父视图控制器,以查看需要保留什么对象或需要正确传递什么消息。
  • Look into Static Analyzer and Instruments for advanced debugging.
  • 查看静态分析仪和仪器以进行高级调试。

I hope this will help you.

我希望这能对你有所帮助。

Regards, Gison

问候,Gison