20

In swift class type has method deinit() in which we can define that instance of class will be removed from memory. How we can know for struct that it will be removed from memory?

For example,

struct Vehicle { ... }
var v: Vehicle? = Vehicle()
v = nil
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Aleksei Danilov
  • 615
  • 1
  • 10
  • 16
  • I don't think that it is duplicated. The question is what analogue of deinit() method for Struct? – Aleksei Danilov Oct 20 '17 at 05:15
  • 1
    Can you add some information *why* you need that information? Perhaps a concrete use-case? – Martin R Oct 20 '17 at 05:16
  • I had been asked this question on interview in context of difference between Class and Struct. – Aleksei Danilov Oct 20 '17 at 05:18
  • If it goes out of scope it will be released automatically – Leo Dabus Oct 20 '17 at 05:29
  • 1
    Here is a possible approach: https://www.reddit.com/r/swift/comments/4lqj64/struct_memory_management/?st=j8zgirr4&sh=d2a0423d (look for "DeallocPointer"), but that does not work if the struct has been copied, because of the value semantics. – See also https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151221/004388.html: *"deinit for a struct doesn't really work because structs are copied and destroyed all the time."* – Martin R Oct 20 '17 at 05:33
  • 1
    @LeoDabus: The struct may hold resources which are *not* memory-managed (such as Unix file descriptors). However, in those case it is better to use a class. – Martin R Oct 20 '17 at 05:34
  • 3
    @MartinR maybe in 5 years from now I might be able to talk about it with you :) – Leo Dabus Oct 20 '17 at 05:37
  • 5
    There is no `deinit` for structs. But `deinit` simply wouldn't have the same utility it does with `class` types. With a class type, if you pass it to another method, you don't really know whether that function might end up keeping a reference to this object, so the code that instantiated the object doesn't really have control over the lifespan of that object. And in that environment, you occasionally need some way to say "by the way, when you're done with this object, this is the sort of cleanup stuff the object needs to do". Value types just don't have these sorts of concerns. – Rob Oct 20 '17 at 06:07
  • @MartinR The possible scenario is this: I am keeping some state info in a struct, and I would want its deist method to persist its contents into a file. Hence, in init() I could parse its contents from the file – rommex Feb 16 '19 at 13:31
  • @rommex: Then a *class* would be more appropriate. – Martin R Feb 16 '19 at 22:10
  • @MartinR but decision between class and struct cannot be based on this consideration. Class is a type, and struct is a value, and this is what dictates the decision – rommex Feb 17 '19 at 11:26

1 Answers1

5

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.

user11630362
  • 158
  • 2
  • 4