Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks fixes #457

Merged
merged 10 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AnimationCache {
}
}

let sceneLayer: CALayer
weak var sceneLayer: CALayer?
var layerCache = [Node: CachedLayer]()

required init(sceneLayer: CALayer) {
Expand Down Expand Up @@ -74,10 +74,10 @@ class AnimationCache {
layer.contentsScale = calculateAnimationScale(animation: animation)

layer.setNeedsDisplay()
sceneLayer.addSublayer(layer)
sceneLayer?.addSublayer(layer)

layerCache[node] = CachedLayer(layer: layer, animation: animation)
sceneLayer.setNeedsDisplay()
sceneLayer?.setNeedsDisplay()

return layer
}
Expand Down Expand Up @@ -138,7 +138,7 @@ class AnimationCache {

let layer = cachedLayer.layer
layerCache.removeValue(forKey: node)
sceneLayer.setNeedsDisplay()
sceneLayer?.setNeedsDisplay()
layer.removeFromSuperlayer()
}

Expand Down
2 changes: 1 addition & 1 deletion Source/bindings/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class Variable<T> {
@discardableResult open func onChange(_ f: @escaping ((T) -> Void)) -> Disposable {
let handler = ChangeHandler<T>(f)
handlers.append(handler)
return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.handlers.index(of: handler) else {
return
}
Expand Down
9 changes: 9 additions & 0 deletions Source/model/scene/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ open class Group: Node {
open var contents: [Node] {
get { return contentsVar.value }
set(val) {

contentsVar.value.forEach { subNode in
nodesMap.remove(subNode)
}

contentsVar.value = val

if let view = nodesMap.getView(self) {
Expand Down Expand Up @@ -35,6 +40,10 @@ open class Group: Node {
self.contentsVar.node = self
}

deinit {
nodesMap.remove(self)
}

// Searching

override public func nodeBy(tag: String) -> Node? {
Expand Down
16 changes: 8 additions & 8 deletions Source/model/scene/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ open class Node: Drawable {
let handler = ChangeHandler<TouchEvent>(f)
touchPressedHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.touchPressedHandlers.index(of: handler) else {
return
}
Expand All @@ -98,7 +98,7 @@ open class Node: Drawable {
let handler = ChangeHandler<TouchEvent>(f)
touchMovedHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.touchMovedHandlers.index(of: handler) else {
return
}
Expand All @@ -111,7 +111,7 @@ open class Node: Drawable {
let handler = ChangeHandler<TouchEvent>(f)
touchReleasedHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.touchReleasedHandlers.index(of: handler) else {
return
}
Expand All @@ -128,7 +128,7 @@ open class Node: Drawable {
tapHandlers[tapCount] = [handler]
}

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.tapHandlers[tapCount]?.index(of: handler) else {
return
}
Expand All @@ -141,7 +141,7 @@ open class Node: Drawable {
let handler = ChangeHandler<TapEvent>(f)
longTapHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.longTapHandlers.index(of: handler) else {
return
}
Expand All @@ -154,7 +154,7 @@ open class Node: Drawable {
let handler = ChangeHandler<PanEvent>(f)
panHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.panHandlers.index(of: handler) else {
return
}
Expand All @@ -167,7 +167,7 @@ open class Node: Drawable {
let handler = ChangeHandler<RotateEvent>(f)
rotateHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.rotateHandlers.index(of: handler) else {
return
}
Expand All @@ -180,7 +180,7 @@ open class Node: Drawable {
let handler = ChangeHandler<PinchEvent>(f)
pinchHandlers.append(handler)

return Disposable { [weak self] in
return Disposable { [weak self, unowned handler] in
guard let index = self?.pinchHandlers.index(of: handler) else {
return
}
Expand Down
6 changes: 5 additions & 1 deletion Source/render/GroupRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class GroupRenderer: NodeRenderer {
updateRenderers()
}

deinit {
dispose()
}

override func doAddObservers() {
super.doAddObservers()

Expand Down Expand Up @@ -53,7 +57,7 @@ class GroupRenderer: NodeRenderer {
override func dispose() {
super.dispose()
renderers.forEach { renderer in renderer.dispose() }
renderers = []
renderers.removeAll()
}

private func updateRenderers() {
Expand Down
4 changes: 4 additions & 0 deletions Source/render/ImageRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ImageRenderer: NodeRenderer {
super.init(node: image, view: view, animationCache: animationCache)
}

deinit {
dispose()
}

override func node() -> Node? {
return image
}
Expand Down
6 changes: 5 additions & 1 deletion Source/render/NodeRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NodeRenderer {
self.view = view
self.animationCache = animationCache

onNodeChange = {
onNodeChange = { [unowned node, weak view] in
guard let isAnimating = animationCache?.isAnimating(node) else {
return
}
Expand All @@ -43,6 +43,10 @@ class NodeRenderer {
addObservers()
}

deinit {
disposables.dispose()
}

func doAddObservers() {
guard let node = node() else {
return
Expand Down
4 changes: 4 additions & 0 deletions Source/render/ShapeRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ShapeRenderer: NodeRenderer {
super.init(node: shape, view: view, animationCache: animationCache)
}

deinit {
dispose()
}

override func node() -> Node? {
return shape
}
Expand Down
4 changes: 4 additions & 0 deletions Source/render/TextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class TextRenderer: NodeRenderer {
super.init(node: text, view: view, animationCache: animationCache)
}

deinit {
dispose()
}

override func node() -> Node? {
return text
}
Expand Down
6 changes: 6 additions & 0 deletions Source/views/NodesMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class NodesMap {
func remove(_ node: Node) {
map.removeObject(forKey: node)
parentsMap.removeValue(forKey: node)

if let group = node as? Group {
group.contents.forEach { child in
self.remove(child)
}
}
}

// MARK: - Parents
Expand Down