EXC_BAD_ACCESS signal received

2018-12-31 08:10发布

When deploying the application to the device, the program will quit after a few cycles with the following error:

Program received signal: "EXC_BAD_ACCESS".

The program runs without any issue on the iPhone simulator, it will also debug and run as long as I step through the instructions one at a time. As soon as I let it run again, I will hit the EXC_BAD_ACCESS signal.

In this particular case, it happened to be an error in the accelerometer code. It would not execute within the simulator, which is why it did not throw any errors. However, it would execute once deployed to the device.

Most of the answers to this question deal with the general EXC_BAD_ACCESS error, so I will leave this open as a catch-all for the dreaded Bad Access error.

EXC_BAD_ACCESS is typically thrown as the result of an illegal memory access. You can find more information in the answers below.

Have you encountered the EXC_BAD_ACCESS signal before, and how did you deal with it?

30条回答
旧人旧事旧时光
2楼-- · 2018-12-31 08:20

This is an excellent thread. Here's my experience: I messed up with the retain/assign keyword on a property declaration. I said:

@property (nonatomic, assign) IBOutlet UISegmentedControl *choicesControl;
@property (nonatomic, assign) IBOutlet UISwitch *africaSwitch;
@property (nonatomic, assign) IBOutlet UISwitch *asiaSwitch;

where I should have said

@property (nonatomic, retain) IBOutlet UISegmentedControl *choicesControl;
@property (nonatomic, retain) IBOutlet UISwitch *africaSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *asiaSwitch;
查看更多
泛滥B
3楼-- · 2018-12-31 08:20

I realize this was asked some time ago, but after reading this thread, I found the solution for XCode 4.2: Product -> Edit Scheme -> Diagnostics Tab -> Enable Zombie Objects

Helped me find a message being sent to a deallocated object.

查看更多
倾城一夜雪
4楼-- · 2018-12-31 08:20

Even another possibility: using blocks in queues, it might easily happen that you try to access an object in another queue, that has already been de-allocated at this time. Typically when you try to send something to the GUI. If your exception breakpoint is being set at a strange place, then this might be the cause.

查看更多
明月照影归
5楼-- · 2018-12-31 08:21

XCode 4 and above, it has been made really simple with Instruments. Just run Zombies in Instruments. This tutorial explains it well: debugging exc_bad_access error xcode instruments

查看更多
若你有天会懂
6楼-- · 2018-12-31 08:22

Use the simple rule of "if you didn't allocate it or retain it, don't release it".

查看更多
怪性笑人.
7楼-- · 2018-12-31 08:22

When you have infinite recursion, I think you can also have this error. This was a case for me.

查看更多
登录 后发表回答