drawRect warning


#1

Present in 3rd edition and 4th edition in drawRect: method in PeopleView.m:
Line: raiseRect.origin.x = NSMaxX(nameRect); generates warning:
“Passed-by-value struct argument contains uninitialized data”

Application compiles and runs ok so I’m not sure I understand warning.


#2

Solution seems to be to add this line to drawRect:

nameRect.origin.y = pageRect.origin.y;


#3

That’s an interesting warning… I see what it means, but for some reason I’m not seeing it here. Another way to fix it would be to initialize the NSRects to NSZeroRect.

It doesn’t affect execution because NSMaxX() only looks at nameRect.origin.x and nameRect.size.width, which are being set.


#4

I also don’t see this warning; however, in looking back at this, using a breakpoint and running the program several times I also see that my nameRect (as well as raiseRect) is starting out with the same “uninitialized” values every time, which is a mystery to me.

I also find it interesting that the values starting out in the uninitialized rectangles to are somewhat related to the pageRect of the PeopleView instance. That is pageRect is (at this point); origin: x=18, y=40 and size: width=576, height=734. Each time nameRect starts out as origin: x=40, y=0 and size: width=734, height=0 and the other raiseRect starts as origin: x=576, y=0 and size: width=18, height=0 before receiving any values in the code following the first two lines in -drawRect:. I am thinking this is more than a coincidence.

The real point is that apparently, at least in mine, the rectangles actually have values before they get to that line mentioned by the OP; although, I have no explanation for why.

  • Lou

#5

Lou,

Stack variables are not initialized to any value; they have the value of whatever was previously at that space in memory. So those similar values are probably not a coincidence. That’s why it’s important to make sure that local (stack) variables are initialized before use. The drawRect: method doesn’t actually use any uninitialized values, but it doesn’t initialize all members of the NSRect struct all at once.

It’s worth noting that under ARC, local Objective-C pointer variables are initialized to nil.

Adam