Re: EXC_BAD_ACCESS
Re: EXC_BAD_ACCESS
- Subject: Re: EXC_BAD_ACCESS
- From: Thomas Davie <email@hidden>
- Date: Thu, 11 Mar 2010 13:49:24 +0000
Your initBezierPath method reallocates and reinitialises a path that's already been created. It also autoreleases the result (as the allocation method doesn't start with alloc, copy, mutableCopy, retain or new).
The result is that the first time 'path' is assigned it gets a non-released value.
The second time that 'path' is assigned, the original value is leaked, as it is never released, and it is given a new value which is autoreleased. By the time you get to your paint method being called, the runloop has completed, and the path has been deallocated.
What you probably want is...
- (id)initSquare // Note, id, not Square, this way, when we subclass, the type system won't explode.
{
self = [super init];
if (nil != self)
{
path = [[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0,10,10) xRadius:5 yRadius:5] retain];
}
return self;
}
- (void)dealloc
{
[path release];
}
Bob
On 11 Mar 2010, at 13:38, Billy Flatman wrote:
> Hi All,
>
> I'm getting a 'EXC_BAD_ACCESS' error when calling the fill on a NSBezierPath.
> Below is an outline of my code.
>
>
> @interface Shape : NSObject {
> NSBezierPath *path;
> }
> - (void)paint;
> @end
>
> @implementation Shape
> - (void) paint {
> [[NSColor redColor] set];
> [path fill];
> }
> @end
>
> @interface Square : Shape <Moveable> {}
> - (Square *) initSquare;
> - (void)initBezierPath;
> @end
>
> @implementation Square
> - (Square *) initSquare {
> self = [super init];
> if (self) {
> path = [[NSBezierPath alloc] init];
> [self initBezierPath];
> }
> return self;
> }
> - (void) initBezierPath {
> path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0,10,10)
> xRadius:5
> yRadius:5];
> [path closePath];
> }
> @end
>
> it works if I change initBezierPath content to:
>
> path = [path init];
> [path appendBezierPathWithRoundedRect:r
> xRadius:5
> yRadius:5];
> [path closePath];
>
> From the main body I initialise a Square and call it's paint method in the drawRect method of an NSView.
>
> Any help would be greatly appreciated.
>
> Billy Flatman
> email@hidden
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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