DrawInRect: withAttributes dies with message posted to freed instance

I'm updating my app for iOS 7. One of the changes is to switch to the new drawInRect: withAttributes function instead of the deprecated drawInRect: withFont ... This works fine on the iOS 7 beta, but today after updating to the latest iOS 7, the app crashes:

[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 tool, which doesn't help at all in either distributing or releasing the object in question 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 is under the caller:

[NSStringDrawingTextStorage stringDrawingTextStorage]

      

What am I doing wrong?

+1


source to share


1 answer


I managed to get around this by trimming leading spaces (including newlines) from the NSString I was doing. 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 keep the newline character, I have no alternative answer.

+1


source







All Articles