Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'garbage value' in 'Left operand of '/' is a garbage value' warning generated by "Build & Analyze"?

When I 'Build and Analyze" this code in Xcode, I get a warning that I don't understand. Here's the method with the problem:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch * touch = [touches anyObject];
        CGPoint location = [touch locationInView:self];
        CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
        [[Stage getSharedStage] movePartToLocation:relativePosition];
}

Here's the warning:

 warning: The left operand of '/' is a garbage value
         CGPoint relativePosition = CGPointMake(1.0-(location.x / self.bounds.size.width),location.y / self.bounds.size.height);
                                                     ~~~~~~~~~~ ^
1 warning generated.

Here are the arrows: alt text

What is it trying to tell me? The code works fine.

like image 686
Michael Forrest Avatar asked Oct 24 '10 12:10

Michael Forrest


3 Answers

It's hard to tell without seeing the entire function, but I would take that to mean that there exists a path the code could take where location.x is never initialized. It may be that the code never takes that path in your testing, but the possibility is there.

EDIT: I'm going to take a wild guess here and say that it's because [touches anyObject] could conceivably return nil. In which case [touch locationInView:self] will return garbage (remember sending messages to nil is perfectly valid).

Try making the rest of the function conditional on (touch != nil).

like image 172
Ferruccio Avatar answered Sep 17 '22 17:09

Ferruccio


If touch is nil, [touch locationInView:] will return the nil struct result, which before some version of llvm would only guarantee that the first [size of pointer] bytes of the struct are zeroed, and the rest would still be garbage.

AFAIK [touches anyObject] can never be nil and the warning is a false positive.

like image 20
nevyn Avatar answered Sep 16 '22 17:09

nevyn


the left operand of '/' is a garbage value ,This suggests tell us that must give an initial value to location.x , because [touch locationInView:self] maybe nil,so Must make a judgment whether the if(touch!=nil)

like image 32
Wanhuagu Avatar answered Sep 16 '22 17:09

Wanhuagu