Possibility of ARC overflow in Swift?

Swift uses Automatic Link Counting to free objects that are no longer referenced and therefore no longer needed. The Swift Reference Guide [1] states the following:

Each time you create a new instance of a class, ARC allocates a piece of memory to store information about this instance. This memory stores information [...] on the values โ€‹โ€‹of any stored properties associated with this instance.

I assume the reference count will be stored as an integer. Can the counter be affected by overflow? If so, then what consequences will this have for my program, will the objects still received by the links from others simply be freed?

As an example: if the counter was, say, an unsigned integer of 2 bytes, this would set the upper limit of the object references (with reference counting still correct) to about 130 thousand. As soon as this upper limit is reached, and the object receives the link once again, this will increment the counter by one, again setting it to 0.

[1] https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

+5
source share
1 answer

NSObject preserveCount is a 64-bit unsigned integer in 64-bit operating systems. And this is a 32-bit unsigned integer on 32-bit OS. This is also the size of the address space, so it is impossible to create many links to one object and overflow the account.

+7
source

Source: https://habr.com/ru/post/1246110/


All Articles