[Drawkit] changing font

Uli Kusterer kusterer at gmail.com
Fri Jun 27 01:14:21 PDT 2008


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







More information about the Drawkit mailing list