Ecuador
Ecuador

Reputation: 1177

drawInRect:withAttributes dies with "message sent to deallocated instance"

I am updating an app for iOS 7. One of the changes is switching to the new drawInRect:withAttributes function instead of the deprecated drawInRect:withFont... This was working fine on the iOS 7 beta, but today after upgrading to the latest iOS 7 version, the app crashes on the line:

[text drawInRect:theRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSz], NSFontAttributeName, color, NSForegroundColorAttributeName, nil]];

With the message:

*** -[NSStringDrawingTextStorage textContainerForAttributedString:containerSize:lineFragmentPadding:]: message sent to deallocated instance 0x187ed0f0

I tried running the Zombie instrument, which is not helpful at all neither the allocation nor the release of the object in question are in my code. Specifically I get the message:

An Objective-C message was sent to a deallocated 'NSStringDrawingTextStorage' object (zombie) at address: 0x169edc50.

And the malloc/release of the object are under the caller:

[NSStringDrawingTextStorage stringDrawingTextStorage]

What am I doing wrong?

Upvotes: 1

Views: 1068

Answers (1)

Eric Harris
Eric Harris

Reputation: 31

I was able to work around this by trimming the leading whitespace (including newline characters) from the NSString I was rendering. Here's my category method:

- (NSString*)stringByTrimmingLeadingWhitespace
{
    NSUInteger index = 0;

    while((index < [self length]) && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember: [self characterAtIndex: index]])
    {
        index++;
    }

    return [self substringFromIndex: index];
}

Unfortunately if you must preserve the leading newline character(s), I do not have an alternative answer.

Upvotes: 1

Related Questions