[swift-users] Checking whether an object is uniquely referenced, using a weak reference to it
Tim Vermeulen
tvermeulen at me.com
Mon Jun 27 20:52:33 CDT 2016
Thanks for your reply. It didn’t clear up everything, though. The official documentation says "Weak references do not affect the result of this function.”, which suggests that weak (and unowned) references intentionally aren’t counted. The docs only mention the implementation of copy-on-write behaviour as a use case (which also happens to be what I’m using it for).
Couldn’t there just a be a function that returns the reference count of a given object as an Int? It would make everything a lot easier (i.e. it wouldn’t need inout because it can just create a reference to that object, find the reference count, then subtract 1).
> Hi, Tim. The purpose of the isUniquelyReferenced checks is to see whether any changes to the properties of the object can be observed from elsewhere in this program. Therefore, I wouldexpectit to return ‘false’ when there are weak (or unowned) references present, i.e. I think the bug is that we don’t consider weak or unowned references to count for this check.
>
> If my understanding of the intended behavior is correct, then it doesn’t make sense to check for a uniquely-referenced object through a weak (or unowned) reference, because if that were the only reference, then the object would have already been deallocated.
>
> The particular technical reason for the check failing is related to the inout, as you say: in order to pass the reference to the function inout, it has to be loaded into a strong reference first. The reason the inout is there at all is because otherwise the check would fail: there would be a reference from outside the function in addition to the one for the argument.
>
> Hope that clears things up, and if you agree with my interpretation about weak and unowned references, please file a bug atbugs.swift.org(http://bugs.swift.org)!
> Jordan
>
>
> > On Jun 27, 2016, at 17:14, Tim Vermeulen via swift-users<swift-users at swift.org(mailto:swift-users at swift.org)>wrote:
> > classEmptyClass {}
> >
> > varstrongReference =EmptyClass()
> > weakvarweakReference =strongReference
> >
> > print(isUniquelyReferencedNonObjC(&strongReference))// true
> > print(isUniquelyReferencedNonObjC(&weakReference))// false
> >
> >
> > I expected both print statements to print true.
> >
> > I realise that this is probably a known limitation of this function. After all, if it worked with weak references too, there’s no point for the parameter to be inout:
> >
> > funcwrappedIsUniquelyReferencedNonObjC<T:AnyObject>(_object:T) ->Bool{
> > weakvarweakObject = object
> > returnisUniquelyReferencedNonObjC(&weakObject)
> > }
> >
> >
> > So the fact that this function takes an inout parameter hints at the fact that it doesn’t work with weak references. If my reasoning is correct, it would be nice if the docs stated that it only works with strong references to objects. :)
> >
> > My question is, is it possible at all to use a weak reference to check whether an object is uniquely referenced or not? Since this approach doesn’t seem to work, I guess it is impossible?_______________________________________________
> > swift-users mailing list
> > swift-users at swift.org(mailto:swift-users at swift.org)
> > https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
More information about the swift-users
mailing list