iabudiab / HTMLKit

An Objective-C framework for your everyday HTML needs.
MIT License
239 stars 27 forks source link

Retain cycle in HTMLNodeIterator #4

Closed dsanghan closed 7 years ago

dsanghan commented 7 years ago

When you do a simple doc.body, the document gets retained by the iterator in root and referenceNode and the iterator gets retained by the document in attachNodeIterator - hence neither gets deallocated.

- (instancetype)initWithNode:(HTMLNode *)node
                 showOptions:(HTMLNodeFilterShowOptions)showOptions
                      filter:(id<HTMLNodeFilter>)filter
{
    self = [super init];
    if (self) {
        _root = node;     // retains doc
        _filter = filter;     // retains doc
        _whatToShow = showOptions;
        _referenceNode = _root;
        _pointerBeforeReferenceNode = YES;
        [_root.ownerDocument attachNodeIterator:self]; // doc retains iterator
    }
    return self;
}

A possible fix is to convert the nodeIterator array into a weak objects NSHashTable.

iabudiab commented 7 years ago

@dsanghan Hey, good catch 👍 Thanks for reporting.

iabudiab commented 7 years ago

The same bug affects HTMLRange. I'll open a new issue for those.

iabudiab commented 7 years ago

Fixed in 2.0.2