Memory Leak Warning on Return

im working on old code, and I have this warning: The structed by-value argument contains uninitialized data (for example, through a field chain: "origin.x"). If I could get help in the dome, I would be very grateful :)

Im code using:

- (void)positionScroller { CGRect screenFrame = [[UIScreen mainScreen] bounds]; CGRect scrollerRect; if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) { scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height ); } else if( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight ) { scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width ); } _scroller.frame = scrollerRect; <---This is where the compiler gives the warning } 

Regards.

+4
source share
3 answers

You can easily get rid of the warning as follows:

 - (void)positionScroller { CGRect screenFrame = [[UIScreen mainScreen] bounds]; CGRect scrollerRect; if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) { scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height ); } else { scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width ); } _scroller.frame = scrollerRect; <---This is where the compiler gives the warning } 
+4
source

The fact is that the compiler cannot be sure that one of the if / else-if blocks is ever reached, in which case scrollerRect will still be uninitialized. You must either add a clean else or initialize a scrollerRect , for example. by installing it on CGRectZero .

By the way, this has nothing to do with a memory leak, it is rather a logical error.

+7
source

You declared CGRect

 CGRect scrollerRect; 

And you assigned a value to this after checking some conditions. If both conditions fail, then it will be without any value. Therefore, he gives a warning. Therefore, add an else clause and set the value to scrollerRect .

So you can have

 if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) { scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height ); } else if( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight ) { scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width ); } else { scrollerRect = CGRectZero; } 
+2
source

Source: https://habr.com/ru/post/1393623/


All Articles