簡體   English   中英

如何解決內存泄漏周期

[英]How to solve memory leak cycle

我怎樣才能解決這種內存泄漏問題? 請幫忙。

@interface ParseOperation () <NSXMLParserDelegate>
// Redeclare appRecordList so we can modify it.
@property (nonatomic, strong) NSArray *appRecordList;
@property (nonatomic, strong) NSData *dataToParse;
@property (nonatomic, strong) NSMutableArray *workingArray;
@property (nonatomic, strong) AppRecord *workingEntry;
@property (nonatomic, strong) NSMutableString *workingPropertyString;
@property (nonatomic, strong) NSArray *elementsToParse;
@property (nonatomic, readwrite) BOOL storingCharacterData;
@end

在此輸入圖像描述

你可以自動發布:

if ([elementName isEqualToString:kEntryStr]) {
    self.workingEntry = [[[AppRecord] new] autorelease];
}

或者簡單地發布:

if ([elementName isEqualToString:kEntryStr]) {
    self.workingEntry = [[AppRecord] new];
    [self.workingEntry release];
}

順便說一句,new與alloc + init相同。

由於ARC已禁用,因此您需要在創建AppRecord實例時自動釋放它。

self.workingEntry = [[[AppRecord alloc] init] autorelease];

首先,它不是保留圈,只是內存泄漏。

泄漏

在你的情況下,workEntry是強大的財產。 這意味着,workingEntry的setter是這樣的:

- (void)setWorkingEntry:(AppRecord *)workingEntry {
    if (workingEntry != _workingEntry){
        [_workingEntry release];
        _workingEntry = [workingEntry retain];
    }
}

正如你在setWorkingEntry之后看到的那樣:屬性的retainCounter至少為1,這將在dealloc中被[_workingEntry release]減少; 並且在正常情況下,保留計數器應為0以釋放該實例,但在您的情況下,您設置[[AppRecord alloc] init](也有+1保留計數器)。 所以在這種情況下,在dealloc中你將使用_workingEntry和retain counter = 2,在你釋放后,retain計數器將變為= 1並且永遠不會被釋放。 因此在設置后釋放以使保持計數器標准化:

if ([elementName isEqualToString:kEntryStr]) {
    self.workingEntry = [[AppRecord] new]; // or [[AppRecord alloc] init]
    [self.workingEntry release];
}

保留圈子

當兩個(或更多)對象彼此具有強引用,並且例如在該類的deallocs中釋放鏈接時,會發生保留圓。 在這種情況下,它們永遠不會被釋放,因為在任何時刻都會有至少一個強大的鏈接(來自其他對象,也無法釋放)。

暫無
暫無

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

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