IOS things about zombie objects and zombie pointers

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.