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

12,943

Solution 1

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).

Solution 2

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.

Solution 3

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)

Share:
12,943
Michael Forrest
Author by

Michael Forrest

Creative Developer and Founder of Good To Hear. goodtohear.co.uk Music software obsessive. github.com/michaelforrest

Updated on August 01, 2022

Comments

  • Michael Forrest
    Michael Forrest almost 2 years

    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.

  • CiscoIPPhone
    CiscoIPPhone over 13 years
    I was thinking along the same lines. What is touch defined as?
  • Michael Forrest
    Michael Forrest over 13 years
    I've expanded to show the whole method. Could it be something to do with using [touches anyObject]?