Possible NSAttributedString bug

View: New views
3 Messages — Rating Filter:   Alert me  

Possible NSAttributedString bug

by Derek Fawcus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I suspect there is a bug here (and maybe in the mutable version as well - I haven't tried it),
in that '-initWithString:attributes:' does not retain the attribute dictionary.

Using the following chunk of code:

static NSTextStorage *textStorage;

void do_tv (void)
{
        NSDictionary *attr;
        NSAttributedString *text;
        NSMutableParagraphStyle *ps =
                [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [ps setAlignment: NSLeftTextAlignment];
        [ps setLineBreakMode: NSLineBreakByCharWrapping];

        attr = [NSDictionary dictionaryWithObjectsAndKeys:
                        ps, NSParagraphStyleAttributeName,
                        nil, nil];
        [ps release];
        text = [[NSAttributedString alloc]
                initWithString:         @"This is the default string.\n"
                attributes:             attr];
        [attr release];

        textStorage = [[NSTextStorage alloc] init];
        [textStorage appendAttributedString: text];
        [text release];

...

I find that the app displaying the text crashes.  Removing the release of attr
prevents the crash, but would seem to leak memory.  The above works on OSX 10.4.11;
and I was finding it failing on base-1.18, gui/back-0.16

From a quick glance at the code,  I suspect something is wrong in GSAttributedString
where the code attempts to 'cache' the attributes,  and refers to making a shallow
copy without copying objects.

DF


_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Re: Possible NSAttributedString bug

by Fred Kiefer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't think this is a bug in GNUstep, there rather is a problem in
your code. You don't own the attr object as it is created via
dictionaryWithObjectsAndKeys: and still you release it.

I am surprised that this works on Apple.

Cheers
Fred

Derek Fawcus schrieb:

> I suspect there is a bug here (and maybe in the mutable version as well - I haven't tried it),
> in that '-initWithString:attributes:' does not retain the attribute dictionary.
>
> Using the following chunk of code:
>
> static NSTextStorage *textStorage;
>
> void do_tv (void)
> {
>         NSDictionary *attr;
>         NSAttributedString *text;
>         NSMutableParagraphStyle *ps =
>                 [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
>         [ps setAlignment: NSLeftTextAlignment];
>         [ps setLineBreakMode: NSLineBreakByCharWrapping];
>
>         attr = [NSDictionary dictionaryWithObjectsAndKeys:
>                         ps, NSParagraphStyleAttributeName,
>                         nil, nil];
>         [ps release];
>         text = [[NSAttributedString alloc]
>                 initWithString:         @"This is the default string.\n"
>                 attributes:             attr];
>         [attr release];
>
>         textStorage = [[NSTextStorage alloc] init];
>         [textStorage appendAttributedString: text];
>         [text release];
>
> ...
>
> I find that the app displaying the text crashes.  Removing the release of attr
> prevents the crash, but would seem to leak memory.  The above works on OSX 10.4.11;
> and I was finding it failing on base-1.18, gui/back-0.16
>
>>From a quick glance at the code,  I suspect something is wrong in GSAttributedString
> where the code attempts to 'cache' the attributes,  and refers to making a shallow
> copy without copying objects.
>
> DF
>
>
> _______________________________________________
> Gnustep-dev mailing list
> Gnustep-dev@...
> http://lists.gnu.org/mailman/listinfo/gnustep-dev
>



_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Re: Possible NSAttributedString bug

by Derek Fawcus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 01, 2009 at 04:42:14PM +0200, Fred Kiefer wrote:
> I don't think this is a bug in GNUstep, there rather is a problem in your code.

Certainly possible,  and given my Obj-C experience not improbable.

> You don't own the attr object as it is created via
> dictionaryWithObjectsAndKeys: and still you release it.

Aha.  It's been autoreleased then I guess?

That stirs a vague memory of reading something that methods with
alloc (and something else?) in their name leave one as the owner;
whereas others don't.

> I am surprised that this works on Apple.

I suspect it simply means the malloc library is tolerant,  not poisioning
memory when it is freed.

Thanks for the correction.

DF


_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev