I'm just getting started with programming GUIs on apple (so far the framework seems fine, but I find the documentation far less informative than others...Qt, .Net, java, etc).
One of the issues I have had is understanding who owns what. For example, if I call CTLineRefCreateWithAttributedString, does the resulting CTLineRef own the attributed string? What if the attributed string is mutable, and I change it? Will this mess up the CTLineRef?
The documentation has been unenlightening.
The reference for CTLineRef provides no information on the subject: https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CTLineRef/Reference/reference.html
Some examples don't release the string, which I take as an indicator that it is owned: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Operations/Operations.html
Some examples do release the string, which would suggest it is not: https://developer.apple.com/library/mac/#samplecode/CoreAnimationText/Listings/VectorTextLayer_m.html
(this one isn't apple, but he seems more informed them I) http://www.cocoanetics.com/2011/01/befriending-core-text/
So is the string copied or not? If I use a CFMutableAttributedStringRef can I change that or not (I assume I would have to create a new CTLineRef afterwards)?
This is a specific example, but this is question that I have come up against in innumerable places. Any help would be very much appreciated. I feel that there must be some rules that govern these things, but I haven't the slightest idea what those rules are or where I can find them.
To answer your question, we can try looking at the string's reference count before and after creating the
CTLine
. We can also try printing the line's description before and after changing the string.It's often fruitless to look at an object's retain count, but in this case it's informative:
Since the retain count is 1 before and after, and I own a reference (because
CFAttributedStringCreateMutable
gives me an owning reference), I know that I am the sole owner of the string, before and after I create theCTLine
. SoCTLine
doesn't retain the string. It's extremely unlikely that it keeps a reference to the string without retaining it.Here's the line's description before changing the string:
I notice that the description doesn't include the string, but does include an array of characters. So the line probably doesn't keep a copy of the string either; it parses the string to create its own private representation.
Here's the line's description after changing the string:
We can see that the line hasn't changed its glyph count or its characters array. From this we can conclude that the line doesn't change when you change the string. You could test further by actually drawing the line before and after changing the string. I leave that as an exercise for the reader.