Here is again an interesting article on the Zombie Objects in Swift.
Firstly, Zombie Objects are something related to memory leaks.
A zombie is an object that has been deallocated from the memory.
Despite being deallocated from the memory they still have references to them.
This can lead to unpredictable behavior like crashes in the application.
Let’s understand it better.
Describing Zombie Objects
Zombies are objects that have the retain cycle 0 but still they exist and hear out the messages.
To detect the Zombie objects we need to enable them in our Xcode project.
Navigate to Edit Schemes -> Diagnostics, and enable the check mark for Zombie objects.
Now you will get the crash log in your console.
There are some characteristics of Zombie Objects which are explained below.
Characteristics of Zombie Objects
If you have enabled the Zombie Objects as described above then you will get a message. For instance, “Message sent to deallocated instance”
Zombie Objects will have the retain cycle of 0.
Zombie Objects are deallocated from the memory but references to it still exist and messages are being sent to it.
Investigating the source of zombie
In addition, to investigate the source of zombie object, look for the answers to the following questions.
1. What was the type of the deallocated object. In addition, what message was sent to it?
2. When was the object actually deallocated?
3. How was the object used after it was deallocated?
You need to consider which objects have strong references and which have weak or unowned references, so that objects are deallocated only when no longer needed and not too soon.
For more details please refer Apple documentation here.
Conclusion
I think you would have got the basic idea for detecting and resolving the crashes related to zombie objects. In Conclusion, I would like you to visit my other blogs here.