1

Just like the title says I'd like to know how I can create a Zombie Object in swift.

The reason I ask is because I'm trying to learn how to use the Zombie Instrument.

I have a sneaking suspicion that there may be Zombie Issues somewhere in my app but I need to verify that I fully understand how to use the instrument first.

I think a good way to do this is

  1. Create a Zombie Object Intentionally
  2. Run Zombie Instruments
  3. Observe the Zombie in Instruments

Edit

Just to be clear, what I'm after here a couple of lines of swift code to demonstrate how zombies actually occur.

My question is 100% related to the suggested duplicate question however the duplicate question asked provided no satisfactory answer.

dubbeat
  • 7,706
  • 18
  • 70
  • 122
  • @Sweeper I dont think its a duplicate. What I'm after is a couple of lines of swift code to demonstrate how zombies actually occur – dubbeat Jul 29 '21 at 12:45
  • 2
    https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk1ch12p495danglingPointer/bk1ch12p495danglingPointer/ViewController.swift – matt Jul 29 '21 at 13:10
  • 1
    @Abizern I can't be certain because this is something I'm trying to learn. But based on Rob Napiers comment about the given example I don't think that this answers my question. – dubbeat Jul 29 '21 at 13:11
  • 2
    I have added another answer to https://stackoverflow.com/questions/53049678/how-can-i-demonstrate-a-zombie-object-in-swift, which might be more what you are looking for. – Martin R Jul 29 '21 at 14:02
  • It's worth knowing that you'd never intentionally create a zombie object yourself even in Objective-C. The runtime turns deallocated objects into zombies (when you enable that) to help you find dangling pointers. This was a common problem in the days of manual memory management because it was easy to unintentionally over-release an object, causing it to be deallocated even though other objects might still expect to have valid pointers to it. Automatic reference counting changed that so that zombies really aren't an important debugging tool even in Objective-C anymore. – Caleb Jul 29 '21 at 14:20
  • 1
    @Caleb: Good point. There is one situation where this may come up in Swift, and that is when you intentionally use `Unmanaged` for the conversion of Swift object pointers to C void pointers and back, in order to “tunnel” them into C callback functions, and you don't choose the correct retained/unretained combination. – Martin R Jul 29 '21 at 14:25
  • @MartinR I should've left "intentionally" out of my comment — you don't directly create zombies yourself at all. They're just the residue of objects that used to exist, left there by the runtime as signposts pointing you to a problem. – Caleb Jul 29 '21 at 14:29

0 Answers0