可可 NSBezierPath - 中风。颜色与预定义颜色不同的崩溃
我有以下问题。我正在编写自己的类,它必须绘制指定大小的圆,并用渐变填充它。
我用一个圆作为 NSBezierPath 来制作它,我使用它绘制渐变
[gradient drawInBezierPath:circle relativeCenterPosition:gradPoint]
所以一切都很好,除了一件事:当我在我的drawRect:代码末尾执行以下命令时:
[borderColor set];
[circle stroke];
我收到错误:(程序收到信号: 的行,
[borderColor set];
“EXC_BAD_ACCESS”)和调试器指向我设置 borderColor
[NSColor colorWithCalibratedRed:0.8 green:0.8 blue:0.8 alpha:1.0]
例如。
只有当我将 borderColor 设置为任何预定义颜色时,它才能正常工作:blackColor、clearColor、greenColor。但我尝试手动设置的任何一个都会崩溃。
有人知道有关颜色的任何限制或可能导致此类问题的东西吗?我没有发现 BezierPath 的描边颜色有什么特别之处。
谢谢。
I have the following problem. I'm writing my own class which must draw a circle of specified size, fill it with gradient.
I made it with a circle being a NSBezierPath, to which I draw my gradient using
[gradient drawInBezierPath:circle relativeCenterPosition:gradPoint]
So everything works great except one thing: when I execute the following command at the end of my drawRect: code:
[borderColor set];
[circle stroke];
I get the error: (Program recieved signal: "EXC_BAD_ACCESS") and debugger points to the line with
[borderColor set];
I set borderColor as
[NSColor colorWithCalibratedRed:0.8 green:0.8 blue:0.8 alpha:1.0]
for example.
It only works normally if i set my borderColor as any predefined color: blackColor, clearColor, greenColor. But any one which I try to set manually, crashes.
Does somebody know about any restrictions about colors or something that could cause such problem? I didn't find anything special about stroke color of BezierPath.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您没有正确保留
borderColor
实例变量。-colorWithCalibrateRed:green:blue:alpha:
方法创建一个自动释放的对象,除非您保留它,否则一旦应用程序事件循环返回,该对象将自动自动释放(并释放)。下一次,当您尝试向已释放的对象发送消息时,就会出现问题。它恰好与
-blackColor
、-clearColor
等一起使用的原因纯粹是运气(某种程度上)。这些“方便颜色”方法恰好返回永远不会被释放的单例实例。例如,如果有人请求-blackColor
1000 次,那么创建 1,000 个单独的实例来浪费内存是没有意义的;相反,返回单个(共享)实例。但是,这是一个私有实现细节,您通常不应依赖它。如何纠正此问题取决于
borderColor
的定义方式。如果它被定义和实现为属性,并且您通过 -setBorderColor: 方法设置它,那么请确保该属性被定义为保留:或者,如果您要设置 borderColor 值通过直接使用实例变量,您应该用保留将其包装:
The problem is that you are not properly retaining the
borderColor
instance variable. The-colorWithCalibratedRed:green:blue:alpha:
method creates an autoreleased object, which, unless you retain it, will be automatically autoreleased (and deallocated) once the application event loop returns. The next time around, when you try to send a message to the deallocated object, problems occur.The reason it happens to be working with
-blackColor
,-clearColor
, etc. is pure luck (sort of). Those "convenience color" methods happen to return singleton instances that will never be deallocated. For example, if someone asks for-blackColor
1000 times, there's no sense in wasting memory by creating 1,000 separate instances; instead, the single (shared) instance is returned. However, this is a private implementation detail that you generally should not rely on.How to correct this depends on how
borderColor
has been defined. If it's defined and implemented as a property, and you're setting it via the-setBorderColor:
method, then make sure the property is defined as a retain:Or, if you're setting the borderColor value by working with the instance variable directly, you should wrap it with a retain: