Re: NSBezierPath retain crash
Re: NSBezierPath retain crash
- Subject: Re: NSBezierPath retain crash
- From: Chad Harrison <email@hidden>
- Date: Wed, 12 Nov 2003 22:36:53 -0500
Thanks all for the reply's - I was having a memory lapse on
autoreleased objects. As stated in my initial email, even when I retain
(and release) properly as suggested, I still crash when operating on
the path. I spit out the lockFocus'ed focusView object to the log, and
it is the same view which is calling this object with the path in it...
I am simply trying to have one of my view's tell this object to
transform and stroke it's path... any other ideas?
thanks
On Nov 12, 2003, at 3:47 PM, Shawn Erickson wrote:
On Nov 12, 2003, at 10:02 AM, Chad Harrison wrote:
I have an object which includes one instance of a NSBezierPath, but
when I try to do anything with the path (like transform or stroke),
it crashes - I though that since I am creating it with
bezierPathWithRect: rect, it was autoreleased and I did not have to
retain it. Even if I specifically retain and release it, it crashes.
I'm missing something here.
You seem to misunderstand Cocoa memory management... read over the
following
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
index.html
@interface MyObject : NSObject {
NSBezierPath *path;
NSAffineTransform *transform;
NSRect rect;
int rectPointW, rectPointH;
}
- (void)setSize:(int)dimension
{
rect.size = NSMakeSize( dimension, dimension);
path = [NSBezierPath bezierPathWithRect: rect];
Path is being allocated by the bezierPathWithRect class method not
directly by your code. So you are getting back path already
autoreleased. So you need to retain path if you want it remain in
existance. "autorelease" mean it will be get a release message sent to
it automatically at some point in the future. This future release
message may result in it being deallocated depending on retain count.
In this case it will result in it being deallocated unless you retain
it.
}
- (void)transform
{
transform = [[NSAffineTransform alloc]
initWithTransform:[NSAffineTransform transform]];
Why are you doing the above? You can simply do the following...
transform = [NSAffineTransform transform];
Also why is "transform" a member variable when it is only used locally
and not cleaned up correctly (you sent it a release message yet leave
the variable pointing at the transform)? So..
NSAffineTransform* transform = [NSAffineTransform transform];
[transform translateXBy:rectPointW yBy:rectPointH];
[path transformUsingAffineTransform: transform]; <----
crashes here
[transform release];
If you switch to simply using [NSAffineTransform transform] then you
do not need to call release since transform was given to you already
autoreleased.
So one possible way to do things (not knowing what you are trying to
do in the end)...
@interface MyObject : NSObject {
NSBezierPath *path;
NSRect rect;
int rectPointW, rectPointH;
}
- (void)dealloc
{
[path release];
}
- (void)setSize:(int)dimension
{
rect.size = NSMakeSize(dimension, dimension);
[path release];
path = [[NSBezierPath bezierPathWithRect: rect] retain];
}
//Ideally you would implement a setPath method that would do the
correct retain/release instead of the above.
- (void)transform
{
NSAffineTransform* transform = [[NSAffineTransform transform]
translateXBy:rectPointW yBy:rectPointH];
[path transformUsingAffineTransform: transform];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.