Bruno
Bruno

Reputation: 1032

Memory leak warning on return

im working on a old code and i have this warning message: Passed-by-value struct argument contains uninitialized data (e.g., via the field chain: 'origin.x'). If i could get dome help i would be very thankful :)

The code im 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
}

Best Regards.

Upvotes: 4

Views: 1842

Answers (3)

Alan Moore
Alan Moore

Reputation: 6575

You can easily get rid of the warning like this:

- (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 
} 

Upvotes: 4

Ilanchezhian
Ilanchezhian

Reputation: 17478

You have declared the CGRect

CGRect scrollerRect;

And you have assigned value to that after checking some conditions. If both of the conditions fail, then it will be without any value. So it is giving the warning. So add else condition and assign value to the 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;
}

Upvotes: 2

omz
omz

Reputation: 53551

The thing is that the compiler can't be sure that one of the if/else-if blocks is ever reached, in which case, scrollerRect would still be uninitialized. You should either add a pure else statement or initialize scrollerRect, e.g. by setting it to CGRectZero.

By the way, this has nothing to do with a memory leak, it's more of a logic error.

Upvotes: 7

Related Questions