2012-01-29 73 views
4

即时通讯工作在旧代码上,并且我有这个警告消息: 通过值结构参数包含未初始化的数据(例如,通过字段链:'origin.x')。如果我能得到穹顶帮助我将非常感激:)使用内存泄漏警告返回

代码IM:

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

问候。

+0

...以及如何定义_scroller? – 2012-01-29 15:45:09

回答

4

您可以轻松地摆脱警告的是这样的:

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

非常感谢您的帮助:) – Bruno 2012-01-30 12:21:25

7

问题在于编译器无法确定if/else-if块中的一个是否已到达,在这种情况下,scrollerRect仍然未初始化。您应该添加纯粹的else声明或初始化scrollerRect,例如通过将其设置为CGRectZero

顺便说一句,这与内存泄漏无关,更多的是逻辑错误。

2

你已经宣布的CGRect

CGRect scrollerRect; 

而且你已经检查了一些情况后,分配的值了这一点。如果两种情况都失败了,那么它就没有任何价值。所以它正在发出警告。因此,添加else条件并将值分配给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); 
} 
else 
{ 
    scrollerRect = CGRectZero; 
}