Skip to content

Commit

Permalink
Fix memory leak in #161 and regression in #194 (#192)
Browse files Browse the repository at this point in the history
Co-authored-by: David Roman <2538074+davdroman@users.noreply.github.com>
  • Loading branch information
okmyself and davdroman authored Feb 15, 2023
1 parent fc40fb1 commit dd06d8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Introspect/UIKitIntrospectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ public struct UIKitIntrospectionView<TargetViewType: UIView>: UIViewRepresentabl
return view
}

/// In some cases, UIView does not call `moveToWindowHandler`,
/// so the `moveToWindowHandler` call in updateUIView must be preserved.
public func updateUIView(
_ uiView: IntrospectionUIView,
context: UIViewRepresentableContext<UIKitIntrospectionView>
) {}
) {
uiView.moveToWindowHandler?()
}

/// Avoid memory leaks.
public static func dismantleUIView(_ uiView: IntrospectionUIView, coordinator: ()) {
uiView.moveToWindowHandler = nil
}
}
#endif
13 changes: 11 additions & 2 deletions Introspect/UIKitIntrospectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,18 @@ public struct UIKitIntrospectionViewController<TargetViewControllerType: UIViewC
return viewController
}

/// If you find that the `moveToWindowHandler` is not called in certain situations (which has not been discovered yet),
/// you can add code to call the `moveToWindowHandler` in the function body.
public func updateUIViewController(
_ uiViewController: IntrospectionUIViewController,
_ viewController: IntrospectionUIViewController,
context: UIViewControllerRepresentableContext<UIKitIntrospectionViewController>
) {}
) {
(viewController.view as? IntrospectionUIView)?.moveToWindowHandler?()
}

/// Avoid memory leaks.
public static func dismantleUIViewController(_ viewController: IntrospectionUIViewController, coordinator: ()) {
(viewController.view as? IntrospectionUIView)?.moveToWindowHandler = nil
}
}
#endif

0 comments on commit dd06d8b

Please sign in to comment.