Introduction
It is a horror to mention zombies, and we all know that zombies are lifeless, but it is really a living creature of the kind that exists. Haha, of course the focus of this article is not about "zombies", but about the zombie pointers (Zombie Pointer) and Zombie objects (Zombie object) that are frequently encountered in iOS.
Wild Hands
First, to introduce the wild pointer, the definition of a wild pointer in C + + is: The wild pointer is a pointer to the garbage memory, the pointer address is not NULL. If you assign a pointer to null, the pointer is a null pointer and can be interpreted with an if statement. However, for wild pointers, you cannot use an if statement to judge.
The reason why the wild pointers are produced
1) The pointer variable is not initialized. Any pointer is not automatically assigned NULL when it is created, and if it is not initialized, the memory address it points to is indeterminate. Therefore, it should be initialized at the time of creation.
char *ptr = NULL; char *str = (char*) malloc (+);
2) After the pointer is released (free or malloc), it is not set to null and is mistakenly assumed to be a legitimate pointer.
void void ) { charnewchar[+]; delete[] str; // Do something " dangerous!! " );
3) The pointer operation is outside the scope of the variable.
class A { public void Func (void ) {cout << "Func of class A" << Endl; }}; void Test (void *p; {A A; P = &a; // note the lifetime of a P ->func (); // P is "wild Pointer" }
When the function Test executes the statement p->func (), object A has disappeared, and P is pointing to a, so p becomes the "wild pointer".
Zombie pointer
A "zombie pointer" is a case of a wild pointer, where the object pointed to by the pointer has been disposed, but the current pointer is not assigned a value of nil.
Zombie Objects
In simple terms, a zombie object is an object that has been disposed of. If you use the object again in your program, you will usually get the following error:
Unrecognized selector sent to instance
Today, I encountered an error like this:
Solution Solutions
You can set the nszombieenabled environment variable in the Xcode scheme page. Click Product-->edit Scheme to open the page, and then tick the Enable Zombie Objects check box. As shown in the following:
The nszombieenabled variable is used to debug memory-related problems and to track the release process of an object. With nszombieenabled enabled, it replaces the default Dealloc implementation with a zombie, which translates the object into a Zombie object when the reference count drops to 0 o'clock. The purpose of a zombie object is that when you send a message to it, it displays a log and automatically jumps into the debugger. When Nszombie is enabled instead of having the app crash directly, an erroneous memory access becomes an unrecognized message to the zombie object. The Zombie object displays the received information and then jumps into the debugger so you can see exactly where the problem is.
Why not turn on zombie object detection by default?
Because once turned on, each time the object is accessed through the pointer, it checks to see if the object pointed to by the pointer is a zombie object. Therefore, it will affect the execution efficiency of the program, it is recommended to close.
IOS things about zombie objects and zombie pointers