[Drawkit] changing font
James Maxwell
jbmaxwell at rubato-music.com
Fri Jun 27 07:16:55 PDT 2008
Ah, you know, I bet that's exactly what happened. I'm using -
fontWithName:size:, and I was aware that it's a factory method - which
is why I used it - but I wasn't clear in my head on when it would
actually be released.
And actually, your explanation also clears up the reason for my "fix"
working; the accessor method I wrote:
- (void) setGlyphFont:(NSFont *) aFont
{
[aFont retain];
[m_font release];
m_font = aFont;
}
would have forced the retain that was missing when I created the font
in the first place.
The links you posted will probably be very helpful. I'd definitely
describe my understanding of Cocoa memory-management as "muddy", at
best. It's slowly getting better, but still pretty sketchy.
Thanks for the links!
cheers,
J.
On 27-Jun-08, at 1:14 AM, Uli Kusterer wrote:
>
> Am 26.06.2008 um 23:19 schrieb James Maxwell:
>
>> Okay, false alarm... I found the problem. Kind of strange, though.
>> I had set the font variable in my init code using a simple
>> assignment. When I changed this to using the actual accessor -
>> setGlyphFont: it worked properly. Does anybody understand why that
>> would be? I'm glad I solved the problem, but it's a little
>> disconcerting that I don't actually know *why* my solution
>> worked. :-\
>
>
> How firm are you in Cocoa memory management? If you did this:
>
> glyphFont = [NSFont fontWithName: @"Lucida Grande"];
>
> with glyphFont being an ivar, then the font has been released by the
> time you get around to using it. The accessor probably releases
> whatever previous value was in glyphFont (if any) and retains the
> new value you put in, so that's why it works. Retaining the value
> keeps it around, but fontWithName: as a factory method gives you an
> autoreleased object at best.
>
> I guess when an NSFont gets released, it clear some of its instance
> variables, and that makes it look like a Helvetica font. The correct
> way to write above line (for an instance variable) would be:
>
> glyphFont = [[NSFont fontWithName: @"Lucida Grande"] retain];
>
> Could that have been your issue? If yes, you may want to re-read
> Apple's documentation on memory management and the naming
> conventions for that:
>
> <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
> >
>
> You may also want to read
>
> <http://www.masters-of-the-void.com/book5.htm>
> <http://www.masters-of-the-void.com/book7.htm>
>
> which is my article on memory management, and the second part
> illustrates what memory bugs do. That should clear up some more
> things. There's also a Podcast with me that covers part of these
> topics at the Mac Developer Network:
>
> <http://www.mac-developer-network.com/podcasts/latenightcocoa/episode10/index.html
> >
>
> This last one is especially useful because it contains links to lots
> of other related articles and Apple documentation on the topic.
>
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de
>
>
>
>
>
> _______________________________________________
> Drawkit mailing list
> Drawkit at lists.apptree.net
> http://lists.apptree.net/listinfo.cgi/drawkit-apptree.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.apptree.net/pipermail/drawkit-apptree.net/attachments/20080627/29a69e79/attachment.htm>
More information about the Drawkit
mailing list