2012-03-08 39 views
1

將項目轉換爲ARC並在泄漏檢測儀中將此功能重複標記爲泄漏。電弧泄漏功能

我在想,objc_release可能是一個解決方案,但xCode不喜歡那樣。

- (int)getNumber{ 
int result = 0; 



unsigned char *myBytes = (unsigned char *)malloc(sizeof(char)); 
[stream getBytes:myBytes range:NSMakeRange(0, sizeof(char))]; 

char tag = myBytes[0]; 

if((int)tag >= 0){ 

    result = (int)tag - 64; 
} 
else if ((int)tag == -64) { 

    [self removeChar]; 

    result = [self getInt]; 


} 
else 
{ 

    [self removeChar]; 
    unsigned char *byteTwo = (unsigned char *)malloc(sizeof(char)); 
    [stream getBytes:byteTwo range:NSMakeRange(0, sizeof(char))]; 
    char twoTag = byteTwo[0]; 

    result = 
    ((((int)tag & 0x03f) << 8) | 
    (twoTag & 0x0ff)) ; 
    result -= 8192; 

} 

return result; 

}

這兩個函數調用的函數內的是

- (void)removeChar{ 
[stream replaceBytesInRange:NSMakeRange(0, 1) withBytes:NULL length:0]; 

}

- (int)getInt{ 
NSRange intRange = NSMakeRange(0,3); 

char buffer[4]; 
[stream getBytes:buffer length:4]; 

[stream replaceBytesInRange:intRange withBytes:NULL length:0]; 


return (int) (
       (((int)buffer[0] & 0xff) << 24) | 
       (((int)buffer[1] & 0xff) << 16) | 
       (((int)buffer[2] & 0xff) << 8) | 
       ((int)buffer[3] & 0xff)); 

}

+2

'malloc' without'free'?這確實看起來像一個泄漏......但也許還有別的事情,我不理解。什麼是靜態分析儀說? – matt 2012-03-08 16:07:59

回答

3

你有沒有擺脫myBytes和byteTwo?我的理解是,Arc不處理該級別的內存管理?

LLVM Arc

+1

是的,ARC不會與'malloc()'混淆,只有對象。 – 2012-03-08 16:14:50

+0

謝謝teamaxe - 免費(myBytes)和免費(byteTwo)清理一切。沒有更多的泄漏! – 2012-03-08 16:30:53