簡體   English   中英

返回時出現內存泄漏警告

[英]Memory leak warning on return

我正在處理一個舊代碼,我有這個警告消息:按值傳遞的struct參數包含未初始化的數據(例如,通過字段鏈:'origin.x')。 如果我能獲得圓頂幫助,我會非常感激:)

我正在使用的代碼:

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

最好的祝福。

問題是編譯器無法確定是否曾到達if / else-if塊之一,在這種情況下, scrollerRect仍然是未初始化的。 您應該添加純的else語句或初始化scrollerRect ,例如通過將其設置為CGRectZero

順便說一句,這與內存泄漏無關,更多的是邏輯錯誤。

您可以輕松地擺脫這樣的警告:

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

您已經聲明了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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM