Resolved Seriously confused - Memory allocation bug ?

Discussion in 'iOS Programming' started by KnightWRX, Mar 16, 2011.

  1. KnightWRX, Mar 16, 2011
    Last edited: Mar 16, 2011

    KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #1
    Ok, this one has got me seriously confused. I'm drawing out some text to a bitmap context using a bundled font. I had the following code in main() to list fonts my application can see :

    Code:
    int main(int argc, char *argv[]) {
        
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	
    	/* List fonts... or not */
    	NSArray * familyNames = [UIFont familyNames];
    	NSArray * fontNames;
    	
    	for(int i = 0; i < [familyNames count]; i++)
    	{
    		NSLog(@"Font family name : %@", [familyNames objectAtIndex: i]);
    		fontNames = [UIFont fontNamesForFamilyName: [familyNames objectAtIndex: i]];
    		for(int j = 0; j < [fontNames count]; j++)
    		{
    			NSLog(@"    Font name : %@", [fontNames objectAtIndex: j]);
    		}
    	}
    
        int retVal = UIApplicationMain(argc, argv, nil, nil);
        [pool release];
        return retVal;
    }
    
    My function to draw out the text then gets called this way :

    Code:
    
    
    -(void) addImageTextFromString: (NSString *) string withColor: (BRUnifiedPixelRGBA) color widthOffset: (NSInteger) widthoffset heightOffset: (NSInteger) heightoffset
    {
    	CGColorSpaceRef colorSpace;
    	CGContextRef context;
    	
    	colorSpace = CGColorSpaceCreateDeviceRGB();
    	context = CGBitmapContextCreate(imagePixels, 
    									width,
    									height,
    									8,
    									width * bytesPerChannel,
    									colorSpace,
    									kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    	
    	CGContextTranslateCTM(context, widthoffset, height - (heightoffset + BR_DEFAULT_FONT_HEIGHT));
    	CGContextSelectFont(context, BR_DEFAULT_FONT_NAME, BR_DEFAULT_FONT_SIZE, kCGEncodingMacRoman);
    	CGContextSetRGBFillColor(context, 
    							 (CGFloat) color.components[COMPONENT_RED] / 255,
    							 (CGFloat) color.components[COMPONENT_GREEN] / 255,
    							 (CGFloat) color.components[COMPONENT_BLUE] / 255,
    							 (CGFloat) color.components[COMPONENT_ALPHA] / 255);
    	CGContextSetTextDrawingMode(context, kCGTextFill);
    	CGContextShowText(context, [string UTF8String], [string length]);
    	CGContextRelease(context);
    	CGColorSpaceRelease(colorSpace);
    	
    }
    
    To make sense of a few types/defines, here's some relevant code from my header :

    Code:
    #define BR_DEFAULT_FONT_NAME    "FontNameCopyPastedFromLog"
    #define BR_DEFAULT_FONT_SIZE    18.0f
    
    #define COMPONENT_RED		   0x00
    #define COMPONENT_GREEN	       0x01
    #define COMPONENT_BLUE         0x02
    #define COMPONENT_ALPHA        0x03
    
    typedef union
    {
    	unsigned char components[4];
    	uint32_t pixel;
    } BRUnifiedPixelRGBA;
    
    This works fine. It shows the text I want it to show at the proper spot. However, if I comment out the font listing code from main, leaving only the 4 lines of auto-generated stuff, the application hangs on the CGContextSelectFont(). Commenting out the call lets the application run (without drawing out any text of course since no font is selected/font size set).

    It doesn't matter if I use my bundled font (which is a .ttf format font) or let's say, plain Helvetica. Commenting out the code from main to list font families/names makes CGContextSelectFont() hang.

    :eek:

    What the heck is the UIFont stuff in main doing that's so special ?
     
  2. KnightWRX, Mar 16, 2011
    Last edited: Mar 16, 2011

    KnightWRX thread starter macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    Should've been more thorough in my Google searches. Seems it's an Apple bug :

    http://www.cocoabuilder.com/archive/cocoa/293268-cgcontextselectfont-spinlock.html

    I get the exact same result as Steve-o, if I pause the debugger, the backtrace is only a spin_lock for the jammed thread. I've also narrowed it down to my single [UIFont familyNames]; call which seems to kick start everything (by somehow pre-loading the system font support).

    He's running on the 4.0 SDK, I'm using the 4.3. Long standing bug it seems.

    No one has run into this one yet ? I wonder what are Apple's plan to fix this.

    Bug is reported : Bug Report #9145915
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    It's rather uncommon to use CG to draw text, and apple discourages this.

    Do you need that because of a custom font?
     
  4. KnightWRX thread starter macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    I need that because I use CG for all my graphics because I need some pixel level access to manipulate my backbuffer. I've read about CGContextSelectFont() being rather slow, but I don't plan on calling it that often.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    I assume you already know that you can't draw anything that's not plain ascii or mac roman that way. Also, pretty sure you can use the normal string drawing methods to draw into a CGContext.
     

Share This Page