Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibility of ARC overflow in Swift?

Swift uses "Automatic Reference Counting" to deallocate objects which are no longer referenced and thus no longer needed. The Swift language guide [1] states the following:

Every time you create a new instance of a class, ARC allocates a chunk of memory to store information about that instance. This memory holds information about [...] values of any stored properties associated with that instance.

I assume that the reference count gets stored as an integer. Could the reference counter then suffer from an overflow ? If this is the case, what implications would this have on my program, would the objects still getting referenced by others simply get deallocated ?

As an example: if the counter were, say, an unsigned 2 byte integer, this would put the upper limit of references to an object (while still having a correct reference count) at around 130k. Once that upper limit is reached and the object gets referenced once more, this would increment the counter by one, setting it to 0 again.

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

like image 376
Benjamin Avatar asked Mar 30 '16 23:03

Benjamin


1 Answers

NSObject's retainCount is a 64-bit unsigned integer on 64-bit OS's. And it's a 32-bit unsigned integer on 32 bit OS's. This is also the size of the address space, so it is impossible to create that many references to a single object and overflow the count.

like image 76
Lou Franco Avatar answered Nov 10 '22 03:11

Lou Franco