Re: Serious bug subclassing NSTextStorage
Re: Serious bug subclassing NSTextStorage
- Subject: Re: Serious bug subclassing NSTextStorage
- From: Todd Ransom <email@hidden>
- Date: Fri, 8 Dec 2006 09:44:20 -0500
Keith,
I have seen the same behavior in my subclasses of NSTextStorage. I
have not been able to find the cause, either. The only advice I can
offer is to check the range and handle the situation gracefully
instead of throwing an exception. If you find any more information
about this please post to the list.
Todd Ransom
Return Self Software
http://returnself.com
On Nov 13, 2006, at 2:21 PM, Keith Blount wrote:
Hello,
I have a subclass of NSTextStorage that does a live word count. I
have been using it in my app as the text storage of the main text
view for over a year with no problems, but recently I have started
to use it also for combining large strings for exporting, and I
have come across a rather serious bug. During a call to -
appendAttributedString on my custom text storage, when there is a
large amount of text (after there have already been several such
calls to -appendAttributedString), I get this exception:
Exception raised during posting of notification. Ignored.
exception: NSMutableRLEArray
replaceObjectsInRange:withObject:length:: Out of bounds
Using a series of NSLogs, I found that the problem seems to be
happening in -setAttributes:range: - somehow, it is getting called
with ranges that are out of bounds of the actual text. This seems
somewhat bizarre, given that the -
replaceCharactersInRange:withString: method is getting called
correctly (with the right ranges etc), and I am not setting
attributes separately (like I say, the bug occurs during a call to -
appendAttributedString:).
I tested this with a plain NSTextStorage, and the exception went
away, so obviously the bug lies in my subclass. I thought this may
be a problem with my word counting methods, so I created a bare
bones NSTextStorage subclass (DummyTextStorage class, the code of
which is included below) and tested with that - and, bizarrely, I
get the same results as with my other subclass - I get the
exception during a call to -appendAttributedString: (and I have
also tried using -replaceCharactersInRange:withAttributedString:
with the same results). My subclass code is the same as I have seen
in various places on the internet (but unfortunately there is no
example in the documentation), so I really don't understand why I
am getting this exception.
If anybody can see where I am going wrong in this code - what would
cause such an exception - I would be _very_ grateful; this one is
really baffling me.
Thanks in advance and all the best,
Keith
-----
@implementation DummyTextStorage
- (id)init
{
if (self = [super init])
{
m_attributedString = [[NSMutableAttributedString alloc] init];
}
return self;
}
- (id)initWithString:(NSString *)aString
{
if (self = [super init])
{
m_attributedString = [[NSMutableAttributedString alloc]
initWithString:aString];
}
return self;
}
- (id)initWithString:(NSString *)aString attributes:(NSDictionary *)
attributes
{
if (self = [super init])
{
m_attributedString = [[NSMutableAttributedString alloc]
initWithString:aString attributes:attributes];
}
return self;
}
- (id)initWithAttributedString:(NSAttributedString *)aString
{
if (self = [super init])
{
m_attributedString = [aString mutableCopy];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]
removeObserver:self]; // According to CocoaDev, we need to do
this...
[m_attributedString release];
[super dealloc];
}
- (NSString *)string
{
return [m_attributedString string];
}
- (NSDictionary *)attributesAtIndex:(unsigned)index effectiveRange:
(NSRangePointer)aRange
{
return [m_attributedString attributesAtIndex:index
effectiveRange:aRange];
}
- (void)replaceCharactersInRange:(NSRange)aRange withString:
(NSString *)aString
{
[m_attributedString replaceCharactersInRange:aRange
withString:aString];
int lengthChange = [aString length] - aRange.length;
[self edited:NSTextStorageEditedCharacters
range:aRange
changeInLength:lengthChange];
}
// Why is this method sometimes getting called with out of bounds
ranges when
// appending an attributed string to the text storage?????
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange
{
[m_attributedString setAttributes:attributes range:aRange];
[self edited:NSTextStorageEditedAttributes
range:aRange
changeInLength:0];
}
- (void)addAttribute:(NSString *)name value:(id)value range:
(NSRange)aRange
{
[m_attributedString addAttribute:name value:value range:aRange];
[self edited:NSTextStorageEditedAttributes
range:aRange
changeInLength:0];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden