NSMutableAttributedString, NSFontAttributeName, and reference counting

  • If I allocate a font object for use with NSMutableAttributedString and
    assign said font to the string using the addAttribute method, will the
    reference count on the font be incremented?  (I assume that if the
    answer is yes, then removeAttribute will decrement the reference count.)

    As a bonus question, can I observe this in the debugger (seems like a
    useful skill in order to avoid similar questions along these lines in
    the future)?  If so, how is that accomplished.

    -Michael
    ----------------------
    The difference between genius and stupidity...
    ...is that genius has its limits."

    -- Albert Einstein
  • On Jan 11, 2009, at 7:59 PM, Michael A. Crawford wrote:

    > If I allocate a font object for use with NSMutableAttributedString
    > and assign said font to the string using the addAttribute method,
    > will the reference count on the font be incremented?  (I assume that
    > if the answer is yes, then removeAttribute will decrement the
    > reference count.)

    Yes, on Leopard & presumably later. On Tiger & earlier, technically
    no, because NSFont objects followed different rules back then; they
    were always around. The Leopard AppKit release notes state this.

    > As a bonus question, can I observe this in the debugger (seems like
    > a useful skill in order to avoid similar questions along these lines
    > in the future)?  If so, how is that accomplished.

    Use Instruments instead of GDB. Its object alloc tracker can
    optionally log retains and releases.

    Nick Zitzmann
    <http://www.chronosnet.com/>