FROM : Andy Lee
DATE : Tue Aug 29 15:33:41 2006
On Aug 28, 2006, at 2:02 PM, Alan Smith wrote:
> Here is the code where the color is retained:
>
> - (void)setHighlightColor:(NSColor*)color
> {
> [highlightColor release];
>
> if (color != nil)
> {
> highlightColor = [color retain];
> }
> }
I don't know if it's related to your crashes, but this code has a
bug. I've removed to NSLog() statements to make it clearer.
Suppose highlightColor == color, meaning both variables point to the
same chunk of memory. If highlightColor's retain count is 1, the
release will cause that chunk of memory to be freed, so both
variables will be dangling pointers. You then proceed to save that
bad pointer in your highlightColor instance variable.
On Aug 28, 2006, at 9:06 PM, Alan Smith wrote:
> What do you
> mean by "accessors"?
Accessors are getter and setter methods for your object's instance
variables -- really for any attributes of your object, but the vast
majority of the time this means instance variables.
> I did read I. Savant replies and I fixed that. It didn't solve the
> problem.
Did you also read Matt's advice about using tried and true memory
management patterns? Shawn gave examples. Take a while to figure
out why those patterns don't have the bug your code has. Then pick
one and memorize it so you can type it without having to think about
it. Or better yet, try Accessorizer.
--Andy
DATE : Tue Aug 29 15:33:41 2006
On Aug 28, 2006, at 2:02 PM, Alan Smith wrote:
> Here is the code where the color is retained:
>
> - (void)setHighlightColor:(NSColor*)color
> {
> [highlightColor release];
>
> if (color != nil)
> {
> highlightColor = [color retain];
> }
> }
I don't know if it's related to your crashes, but this code has a
bug. I've removed to NSLog() statements to make it clearer.
Suppose highlightColor == color, meaning both variables point to the
same chunk of memory. If highlightColor's retain count is 1, the
release will cause that chunk of memory to be freed, so both
variables will be dangling pointers. You then proceed to save that
bad pointer in your highlightColor instance variable.
On Aug 28, 2006, at 9:06 PM, Alan Smith wrote:
> What do you
> mean by "accessors"?
Accessors are getter and setter methods for your object's instance
variables -- really for any attributes of your object, but the vast
majority of the time this means instance variables.
> I did read I. Savant replies and I fixed that. It didn't solve the
> problem.
Did you also read Matt's advice about using tried and true memory
management patterns? Shawn gave examples. Take a while to figure
out why those patterns don't have the bug your code has. Then pick
one and memorize it so you can type it without having to think about
it. Or better yet, try Accessorizer.
--Andy






Cocoa mail archive

