Categories
Swift

Swift Debug EXC_BAD_ACCESS in AppDelegate

 

Many people will encounter in the development EXC_BAD_ACCESS ,I'm sorry,This time, Xcode does not give any detailed solutions。

usually,This is due to a memory error caused。In simple terms it is that you create an object A,But in the later time of the visit,A memory in this area have been moved to do the system he used,Put objects such as B here - A your left hand,The actual content does not exist。

Then there will be such a collapse similar:

Since the actual object has changed,Swift compiler does not know,The result is naturally an "unknown Selector"。

Try to solve it

At the beginning,Ignorant I want to find who this "instance" is,So I started to use this method to print the objects in the program:

Ok,Later I found out that this is an endless job ... but the error becomes interesting:

This makes me even more puzzled。

after that,I did n’t make a mistake.,It is EXC_BAD_ACCESS ,So the memory address comparison ends here ...

Correct approach

In short,Xcode still has tools to deal with this situation-although not necessarily all,But at least it can add a little clue to troubleshooting,Let ’s go to the upper left corner of Xcode and select Edit Scheme,Edit current program execution options:

Select Edit Scheme .... To edit the operating parameters
Choose Edit Scheme… To edit the operating parameters

Select "Run" on the opened page,Select the "Diagnosis" tab on the right,Check the "Zombie Object" below。

Turn on Zombie Object mode

Zombie Object Mode:Zombie mode,In this mode your program will not really release the memory that needs to be released,Even if object A has no reference,It will also be kept in memory,This prevents the system from writing the memory of other objects to the same location,once EXC_BAD_ACCESS occur,Xcode will be able to discover which object the program originally wanted to access。

Run the program again,Trigger a crash,We got a new error:

Obviously,This time it's much clearer,Although somehow,But this time EXC_BAD_ACCESS Is because the program accessed a NSSpellChecker Object-induced,This greatly reduces the scope of troubleshooting,If you are lucky, you should be able to locate the problem soon。

In short,Finally remember to turn off this mode,Otherwise the memory occupied by the program will increase endlessly ...

References

Original article written by Gerber drop-off:R0uter's Blog » Swift Debug EXC_BAD_ACCESS in AppDelegate

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3313.html

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *