2

I am not sure what is going on anymore. I have tried so many things to for me to figure out whats going on on this specific line of code that is causing a EXC_BAD_ACCESS. I tried enabled NSZombies but it didn't help me any. Here is the code:

- (int)linesFromText:(NSString *)string withFont:(UIFont *)font andSize:(CGSize)size {
    NSArray *splitString = [string componentsSeparatedByString:@" "];
    NSMutableArray *allLines = [NSMutableArray array];
    NSMutableString *line = [NSMutableString string];
    NSString *word;
    NSString *fakeLine;
    for (int i = 0; i < splitString.count; i++) {

        word = [splitString objectAtIndex:i];
        fakeLine =  [NSString stringWithFormat:@"%@%@ ",line, word];
        //NSLog(@"line %@, font %@",fakeLine,font);

        ////THIS IS THE LINE CAUSING THE EXC_BAD_ACCESS
        CGSize lineSize = [fakeLine sizeWithFont:font];

        if (lineSize.width <= size.width) {
            [line appendFormat:@"%@ ", word];
        } else {
            [allLines addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            line = [NSMutableString string];
            [line appendFormat:@"%@ ", word];
        }


    }
    [allLines addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
    return allLines.count;
}

This is driving me crazy cause it only happens on the new iphone 5 when you scroll through the days to fast in the app I built. Here is a link to the application in the store:

http://itunes.apple.com/us/app/id543324451?mt=8

if you have an iphone 5 you can see what I mean. iphone 4 doesn't do this.

This code is being called in a UITableViewCell's layoutSubviews and is there to help size the frame of a Custom Attributed label that uses TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel).

I also tried to enable "Gaurd malloc to try give me more details but my XCode gives me this error:

dyld: could not load inserted library '/usr/lib/libgmalloc.dylib' because image not found

which if I look in /usr/lib that file is a sym link to a file that does exist in the same directory: libgmalloc.dylib -> libgmalloc.B.dylib

I am running out of ideas here and thought it might be the UIFont getting released to soon and then it not being available so I put references in the UITableViewCell to hold the UIFont until the end of that Cell's life.

I have also searched around the internet and haven't found that much on this specifics.

Also here is an image of my stack trace from debugger:

https://i.stack.imgur.com/gWC5L.png

Any ideas? Did I provide enough info?

Thanks

Dev Goose
  • 55
  • 6
  • 1
    I would say it almost definitely is to do with the font. To test this, replace it with a local UIFont variable. If the problem is gone when using a local variable, then you know where your problem lies. – Bergasms Oct 02 '12 at 05:02
  • I tried setting it as a local variable and it still had issues. I will try a different approach for the local variable tomorrow and see if there is an issue there. – Dev Goose Oct 02 '12 at 05:24
  • have you set a breakpoint to see if the font is nil at the point you are trying to use it? That would be a quick way to see if something is wrong with it. – Dima Oct 02 '12 at 06:16
  • yes, I tried adding a break point and as you can see I was logging out the font as well in that comment. – Dev Goose Oct 02 '12 at 16:36
  • @Bergasms I think your right Bergasms,I think it definitely has to do with the UIFont not being there when it actually gets down into the sizeWithFont method. I think its a race condition and why I am still seeing font get spit out to NSLog in the line before. When I moved it to a local variable it definitely crashed a lot less, but still crashed. – Dev Goose Oct 02 '12 at 18:29
  • Was it still on the same line? To make sure it is definitely that line (as opposed to that line just triggering the error to be thrown) comment the line out and just set linesize to some value. Does it still crash after that? – Bergasms Oct 02 '12 at 23:10

1 Answers1

0

I think your question is answered here:

UIStringDrawing methods don't seem to be thread safe in iOS 6

The short version: sizeWithFont, and most other UIKit methods, are not thread safe when you're using it on the screen (rather than using it to pre-render).

The good news: look at Adam Swinden's answer in that thread; he explains how to get the same result in iOS6 with CoreText instead of UIKit.

Community
  • 1
  • 1
chapka
  • 490
  • 1
  • 3
  • 11