iPhone上的NSString drawAtPoint崩溃(NSString drawAtPoint)

时间:2010-04-02 07:19:57

标签: iphone debugging crash nsstring

嘿。我有一个非常简单的文本输出到缓冲系统,将随机崩溃。这对DAYS来说没问题,有时它会在几分钟内崩溃几次。对于使用更高级别控件的其他人来说,callstack几乎完全相同: http://discussions.apple.com/thread.jspa?messageID=7949746 iPhone app crashed: Assertion failed function evict_glyph_entry_from_strike, file Fonts/CGFontCache.c

它崩溃了(在下面以及drawTextToBuffer()中): [nsString drawAtPoint:CGPointMake(0,0)withFont:clFont];

我对“evict_glyph_entry_from_cache”的调用与紧随其后的中止调用相同。

显然它发生在其他人身上。我可以说我的NSString *在崩溃时完全没问题。我可以很好地阅读调试器中的文本。

static CGColorSpaceRef curColorSpace;
static CGContextRef myContext;
static float w, h;
static int iFontSize;
static NSString* sFontName;
static UIFont* clFont;
static int iLineHeight;

unsigned long* txb; /* 256x256x4 Buffer */


void selectFont(int iSize, NSString* sFont)
{
    iFontSize = iSize;
    clFont = [UIFont fontWithName:sFont size:iFontSize];
    iLineHeight = (int)(ceil([clFont capHeight]));
}


void initText()
{
    w = 256;
    h = 256;

    txb = (unsigned long*)malloc_(w * h * 4);

    curColorSpace = CGColorSpaceCreateDeviceRGB();
    myContext = CGBitmapContextCreate(txb, w, h, 8, w * 4, curColorSpace, kCGImageAlphaPremultipliedLast);

    selectFont(12, @"Helvetica");
}

void drawTextToBuffer(NSString* nsString)
{
    CGContextSaveGState(myContext);
    CGContextSetRGBFillColor(myContext, 1, 1, 1, 1);
    UIGraphicsPushContext(myContext);

    /* This line will crash.  It crashes even with constant Strings..   At the time of the crash, the pointer to nsString is perfectly fine.  The data looks fine! */
    [nsString drawAtPoint:CGPointMake(0, 0) withFont:clFont];
    UIGraphicsPopContext();
    CGContextRestoreGState(myContext);
}

它将与其他非unicode支持方法一起发生,例如CGContextShowTextAtPoint(); callstack也与此类似。

这是iPhone的任何已知问题吗?或者,也许,在这个特定的调用(drawAtPoint)之外的某些事情可能导致异常吗?

1 个答案:

答案 0 :(得分:2)

void selectFont(int iSize, NSString* sFont)
{
    iFontSize = iSize;
    clFont = [UIFont fontWithName:sFont size:iFontSize];
    iLineHeight = (int)(ceil([clFont capHeight]));
}

调用此函数后,clFont将被自动释放,因此无法保证与此函数无关的上下文仍然可以具有有效的clFont。如果您打算稍后使用它,则需要-retain个实例。

void selectFont(int iSize, NSString* sFont)
{
    iFontSize = iSize;
    [clFont release];
    clFont = [[UIFont fontWithName:sFont size:iFontSize] retain];
    iLineHeight = (int)(ceil([clFont capHeight]));
}