Skip navigation.
 
mlRe: I'm having trouble retaining a NSColor
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

Related mailsAuthorDate
mlI'm having trouble retaining a NSColor Alan Smith Aug 28, 20:02
mlRe: I'm having trouble retaining a NSColor I. Savant Aug 28, 20:11
mlRe: I'm having trouble retaining a NSColor I. Savant Aug 28, 20:14
mlRe: I'm having trouble retaining a NSColor Matt Neuburg Aug 28, 20:27
mlRe: I'm having trouble retaining a NSColor Shawn Erickson Aug 28, 20:42
mlRe: I'm having trouble retaining a NSColor Alan Smith Aug 29, 00:36
mlRe: I'm having trouble retaining a NSColor Rosyna Aug 29, 00:52
mlRe: I'm having trouble retaining a NSColor Alan Smith Aug 29, 01:48
mlRe: I'm having trouble retaining a NSColor Steve Bird Aug 29, 01:57
mlRe: I'm having trouble retaining a NSColor Ricky Sharp Aug 29, 02:25
mlRe: I'm having trouble retaining a NSColor Alan Smith Aug 29, 03:06
mlRe: I'm having trouble retaining a NSColor Shawn Erickson Aug 29, 03:13
mlRe: I'm having trouble retaining a NSColor Andy Lee Aug 29, 15:33
mlRe: I'm having trouble retaining a NSColor Matt Neuburg Aug 29, 19:09
ml[SOLVED] I'm having trouble retaining a NSColor Alan Smith Sep 5, 05:20