NSAttributedString attribute:atIndex:effectiveRange crash

  • I'm using the following code to figure out where a click occurred in
    an attributed string that's displayed.

    // Now layout the string to figure out where in the string the click
    occurred
    NSTextStorage *textStorage = [[NSTextStorage alloc]
    initWithAttributedString: string];
    [string release];
    NSTextContainer *textContainer = [[NSTextContainer alloc]
    initWithContainerSize: [textStorage size]];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    [layoutManager addTextContainer: textContainer];
    [textStorage addLayoutManager: layoutManager];

    // force layout
    [layoutManager glyphRangeForTextContainer: textContainer];

    int glyphIndex = [layoutManager glyphIndexForPoint: [point pointValue]
                                         inTextContainer: textContainer];
    int charIndex = [layoutManager characterIndexForGlyphAtIndex:
    glyphIndex];

    [layoutManager release];
    [textContainer release];

    NSRangePointer range;
    id attribute = nil;
    if (charIndex < [textStorage length])
      attribute = [textStorage attribute: NSLinkAttributeName
                                     atIndex: charIndex
          effectiveRange: range];
    NSURL *url = nil;
    if (attribute)
      url = [attribute copy];
    [textStorage release];

    The problem is that half the time it crashes with EXC_BAD_ACCESS  and
    this stack trace:

    #0    0x927fff1e in blockForLocation
    #1    0x927ffe9a in -[NSRLEArray objectAtIndex:effectiveRange:]
    #2    0x928003fc in -[NSConcreteMutableAttributedString
    attribute:atIndex:effectiveRange:]
    #3    0x93377c7f in -[NSConcreteTextStorage
    attribute:atIndex:effectiveRange:]

    I can't seem to find any info related to blockForLocation or figure
    out what the problem is.  Any help would be much appreciated.

    Thanks in advance,

    Greg
  • Your range pointer is not pointing to a valid structure. Try:

    On Oct 16, 2007, at 6:02 PM, Greg Hoover wrote:

    > NSRangePointer range;

    NSRange range;

    > id attribute = nil;
    > if (charIndex < [textStorage length])
    > attribute = [textStorage attribute: NSLinkAttributeName
    > atIndex: charIndex
    > effectiveRange: range];

      effectiveRange: &range];

    > NSURL *url = nil;
    > if (attribute)
    > url = [attribute copy];
    > [textStorage release];
    >
    > The problem is that half the time it crashes with EXC_BAD_ACCESS
    > and this stack trace:
    >
    > #0    0x927fff1e in blockForLocation
    > #1    0x927ffe9a in -[NSRLEArray objectAtIndex:effectiveRange:]
    > #2    0x928003fc in -[NSConcreteMutableAttributedString
    > attribute:atIndex:effectiveRange:]
    > #3    0x93377c7f in -[NSConcreteTextStorage
    > attribute:atIndex:effectiveRange:]
    >
    > I can't seem to find any info related to blockForLocation or figure
    > out what the problem is.  Any help would be much appreciated.
    >
    > Thanks in advance,
    >
    > Greg
  • Thank you thank you thank you!

    Greg
    On Oct 16, 2007, at 3:16 PM, Dave Fernandes wrote:

    > Your range pointer is not pointing to a valid structure. Try:
    >
    > On Oct 16, 2007, at 6:02 PM, Greg Hoover wrote:
    >
    >> NSRangePointer range;
    >
    > NSRange range;
    >
    >> id attribute = nil;
    >> if (charIndex < [textStorage length])
    >> attribute = [textStorage attribute: NSLinkAttributeName
    >> atIndex: charIndex
    >> effectiveRange: range];
    >
    > effectiveRange: &range];
    >
    >
    >> NSURL *url = nil;
    >> if (attribute)
    >> url = [attribute copy];
    >> [textStorage release];
    >>
    >> The problem is that half the time it crashes with EXC_BAD_ACCESS
    >> and this stack trace:
    >>
    >> #0    0x927fff1e in blockForLocation
    >> #1    0x927ffe9a in -[NSRLEArray objectAtIndex:effectiveRange:]
    >> #2    0x928003fc in -[NSConcreteMutableAttributedString
    >> attribute:atIndex:effectiveRange:]
    >> #3    0x93377c7f in -[NSConcreteTextStorage
    >> attribute:atIndex:effectiveRange:]
    >>
    >> I can't seem to find any info related to blockForLocation or
    >> figure out what the problem is.  Any help would be much appreciated.
    >>
    >> Thanks in advance,
    >>
    >> Greg
    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today