Enable Scribble,Enable Guard Edges,Enable Guard Malloc,Zombie Objects

最近项目中使用一个翻拍身份证信息识别活体检测的第三方框架,在使用时会偶然性的出现崩溃的现象,经过查找是因为第三方框架中有释放的内存区域再次引用引起的,因而补充一下相关知识点。

 
在Xcode Edit Scheme中有这样几个属性:
 
Enable Scribble,Enable Guard Edges,Enable Guard Malloc,Zombie Objects
 
下面是我在网上找到的相关英文的解释,非专业出身,简单翻译一下用于理解
 
Enable Scribble: Fill allocated memory with 0xAA and deallocated memory with 0x55.
Scribble will make it rather obvious that you're using a memory block after it's free'd by overwriting any data that used to be in the memory block upon free.
启用数据写入:对象释放时,所用内存并没有完全被擦除,仍有旧对象部分数据可用,如果不使用Zombie调试,App可能不会直接crash。对付这种情况,其实很简单,可以在对象内存释放时写入无意义数据分配的内存写入数据0xAA,释放的内存写入0x55。有些内存块已经被释放时,可以通过对数据进行重写让你继续使用内存块。
   
Enable Guard Edges: Add guard pages before and after large allocations.
启用保护边缘:在大的内存分配之前或者之后添加保护页面。
  
Enable Guard Malloc: Use libgmalloc to catch common memory problems such as buffer overruns and use-after-free.
启用内存保护:利用libgmalloc来捕获类似于内存溢出,对释放内存的对象再引用等内存问题。
 
Guard edges and Guard Malloc will help you find memory overruns and (to some extent) use-after-free by read and write protecting memory blocks to make your program crash more obviously if misusing memory.
边缘保护和内存分配保护,可以用来帮你查找程序中的内存问题,当代码中出现内存溢出,释放后再使用的内存问题时,它会让程序crash,这样会使得内存问题表现的更为明显。
 
僵尸对象(Zombie Objects):如果打开了ARC或垃圾回收模式,在程序中发消息给已经释放的对象,将会引起程序崩溃。这时定位崩溃原因将非常困难,因为出问题的对象已经重新分配了。

解决方法:可以通过启动僵尸对象(Zombie Objects)来解决,开启该选项后,程序在运行时,如果访问了已经释放的对象,则会给出较准确的定位信息,可以帮助确定问题所在。

功能的原理:在对象释放(retainCount 为0)时,使用一个内置的Zombie对象,替代原来被释放的对象。无论向该对象发送什么消息(函数调用),都会触发异常,抛出调试信息。

注意:记得问题被修复后,关闭该功能