I have a pretty simple class:
class TestClass {}
then I created an instance of this class:
let instance = TestClass()
After that I started my project (debug build) and ran command in Xcode console:
language swift refcount instance
and what I got was:
refcount data: (strong = 3, unowned = 1, weak = 1)
I wonder why my instance has 3 strong references, 1 unowned and 1 weak, if in fact there is only 1 strong reference.
The reason language swift refcount instance
is showing 3 strong references is probably because of retaining your instance inside of debug code. Moreover, if you will repeat the command, you will get incrementing results:
(lldb) language swift refcount instance
refcount data: (strong = 3, unowned = 1, weak = 1)
(lldb) language swift refcount instance
refcount data: (strong = 4, unowned = 1, weak = 1)
(lldb) language swift refcount instance
refcount data: (strong = 5, unowned = 1, weak = 1)
I think this is kind of Xcode/runtime bug.
Also, you can use CFGetRetainCount
to check reference count on an instance:
let instance = TestClass()
print(CFGetRetainCount(instance)) // prints "2"
The reason you get "2" is instance
is retained by let instance
static variable, as well as it is retained inside the CFGetRetainCount
function. You can proof this by calling
print(CFGetRetainCount(TestClass())) // prints "1"