简体   繁体   English

如何解决内存泄漏周期

[英]How to solve memory leak cycle

How can I solve this kind memory leak problem? 我怎样才能解决这种内存泄漏问题? Please help. 请帮忙。

@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

在此输入图像描述

You can either autorelease: 你可以自动发布:

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

or simply release: 或者简单地发布:

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

By the way, new is same as alloc + init. 顺便说一句,new与alloc + init相同。

由于ARC已禁用,因此您需要在创建AppRecord实例时自动释放它。

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

First of all it's not a retain circle, it's just memory leak. 首先,它不是保留圈,只是内存泄漏。

Leak 泄漏

In your situation workingEntry is strong property. 在你的情况下,workEntry是强大的财产。 That mean, that setter of workingEntry is something like: 这意味着,workingEntry的setter是这样的:

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

As you can see after setWorkingEntry: property will have retainCounter at least 1, that will be decreased in dealloc by [_workingEntry release]; 正如你在setWorkingEntry之后看到的那样:属性的retainCounter至少为1,这将在dealloc中被[_workingEntry release]减少; and in normal case retain counter should be 0 to free that instance, but in you case you set [[AppRecord alloc] init] (that also have +1 retain counter). 并且在正常情况下,保留计数器应为0以释放该实例,但在您的情况下,您设置[[AppRecord alloc] init](也有+1保留计数器)。 So in this case in dealloc you will have _workingEntry with retain counter = 2, after you release it, retain counter will became = 1 and will never be freed. 所以在这种情况下,在dealloc中你将使用_workingEntry和retain counter = 2,在你释放后,retain计数器将变为= 1并且永远不会被释放。 So release in after setting to make retain counter normalized: 因此在设置后释放以使保持计数器标准化:

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

Retain circle 保留圈子

Retain circle occurs when two (or more) objects have strong references to each other, and that links released, for example, in deallocs of that classes. 当两个(或更多)对象彼此具有强引用,并且例如在该类的deallocs中释放链接时,会发生保留圆。 In such situation they will never be released, because there will be at least one strong link to them at any moment (from other object, that also can't be freed). 在这种情况下,它们永远不会被释放,因为在任何时刻都会有至少一个强大的链接(来自其他对象,也无法释放)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM