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:
1 |
-[__NSCFType dismissAuxiliaryWindows]: unrecognized selector sent to instance 0x6000030010e0 |
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:
1 2 |
var a = 1 print(Unmanaged.passUnretained(a).toOpaque()) |
Ok,Later I found out that this is an endless job ... but the error becomes interesting:
1 |
-[_TtGCs23_ContiguousArrayStorageSS_$ dismissAuxiliaryWindows]: unrecognized selector sent to instance 0x600003004240 |
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 "Run" on the opened page,Select the "Diagnosis" tab on the right,Check the "Zombie Object" below。
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:
1 |
*** -[NSSpellChecker dismissAuxiliaryWindows]: message sent to deallocated instance 0x600003010b40 |
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 LogStudio: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