Does NSPasteboard retain owner objects?

不羁岁月 提交于 2019-12-08 07:10:48

问题


You can call NSPasteboard like this:

[pboard declareTypes:types owner:self];

Which means that the pasteboard will later ask the owner to supply data for a type as needed. However, what I can't find from the docs (and maybe I've missed something bleeding obvious), is whether or not owner is retained.

In practice what's worrying me is if the owner is a weak reference, it could be deallocated, causing a crash if the pasteboard then tries to request data from it.

Note: I should probably clarify that I'm interested in this more as an aid to tracking down a bug, than making my app rely on it. But I do also want the docs clarified.


回答1:


The docs:

newOwner

The object responsible for writing data to the pasteboard, or nil if you provide data for all types immediately. If you specify a newOwner object, it must support all of the types declared in the newTypes parameter and must remain valid for as long as the data is promised on the pasteboard.

Translation: The pasteboard may or may not retain the owner. Whether it does is an implementation detail that you should not rely upon. It is your responsibility to retain the owner for as long as it acts as an owner.

What the docs are saying about "remain valid" actually refers to the proxied contents that you might lazily provide. I.e. if the user were to copy something, you wouldn't want the owner's representation of what was copied to change as the user makes further edits with an intention of pasting sometime later.

The documentation says nothing about the retain/release policy of the owner (nor is there any kind of blanket rule statement). It should be clarified (rdar://8966209 filed). As it is, making an assumption about the retain/release behavior is dangerous.



来源:https://stackoverflow.com/questions/4920952/does-nspasteboard-retain-owner-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!