Random EXC_BAD_ACCESS in a place that it cannot ha

2019-01-20 16:12发布

问题:

I'm receiving EXC_BAD_ACCESS at random times in a place that I confirmed the exception should not happen.
The line its happening on is

`[[LevelEditorScene sharedLevelEditor] setObject:object forKey:key]`

LevelEditorScene is a class, setObject:forKey: is a method I implemented (not the one from NSObject)
In GDB:

(gdb) print-object [LevelEditorScene sharedLevelEditor]
<LevelEditorScene = 06C3FF40 | Tag = -1>

Means LevelEditorScene sharedLevelEditor is ok.

(gdb) print (bool)[[LevelEditorScene sharedLevelEditor]
                    respondsToSelector:@selector(setObject:forKey:)]
$9 = true

Means it responds to the selector

(gdb) print-object object
15
(gdb) print-object key
Maze Width

Means the arguments are ok (it shouldn't matter anyway).
What else can be causing the exception?

回答1:

The line of code that your app crashes on might not be the cause of the problem. Most likely your method call is fine. Otherwise you'd receive a "message sent to unrecognised selector" error.

The best steps are to run the Analyzer. Failing that Profile using Leaks in Instruments.

A brilliant description on EXC_BAD_ACCESS, what causes it, and how to debug it can be found here:

Lou Franco's Understanding EXC_BAD_ACCESS