How in Swift to know that struct is deleted from Memory? How in Swift to know that struct is deleted from Memory? ios ios

How in Swift to know that struct is deleted from Memory?


Structs are deallocated when they go out of scope. You can't put a deinit in a struct, but here is a workaround. You can make a struct that has a reference to a class that prints something when deallocated.

class DeallocPrinter {    deinit {        print("deallocated")    }}struct SomeStruct {    let printer = DeallocPrinter()}  

So when the struct is deallocated - if you haven't made a copy of the struct, it'll print deallocated when it's deallocated, since the DeallocPrinter will be deallocated at the same time the struct is deallocated.


A simple way is the using of a dummy class. Just create an empty class and implement there the deinit(). Then use this class in your struct as member, p.e.

let dummyClass = DummyClass()

Once the structure is released, the deinit() function of the class is called. If not, then you have a memory leak.