Window hide doesn't deallocate memory

125 Views Asked by At

I have presented a custom UIWindow in order to show some screens. When I hide the window, the window instance doesn't free up. The underlying view controller also not getting released.

Custom window logic :

class SDKWindow: UIWindow {

// Unique tag to find the SDKWindow in the containers. Iterate all the windows and find the sdk window to show/hide any controller.
static let SDKWindowTag = 101011

init(rootController: UIViewController) {
    if #available(iOS 13.0, *) {
        let windowScene = UIApplication.shared
            .connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .first

        if let windowScene = windowScene as? UIWindowScene {
            super.init(windowScene: windowScene)
        } else {
            super.init(frame: UIScreen.main.bounds)
        }

    } else {
        super.init(frame: UIScreen.main.bounds)
    }
    self.rootViewController = rootController
    self.windowLevel = .statusBar - 1
    self.tag = SDKWindow.SDKWindowTag
    self.accessibilityLabel = "SDK.Window"
    self.accessibilityViewIsModal = true
    
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func show() {
    printLog("Show sdkwindow")
    self.makeKeyAndVisible()
    if self.isHidden == false {
        return
    }
    self.isHidden = false
    self.alpha = 1.0
}

func hide() {
    if #available(iOS 13.0, *) {
        self.windowScene = nil
    } else {
        self.rootViewController = nil
    }
    self.alpha = 0.0
    self.isHidden = true
    self.resignKey()
    self.removeFromSuperview()
    
    
}

}

When the window presented is hidden

@objc public class MyClass: NSObject {
    
    
    /// Private variables
    private var popupWindow: SDKWindow?

public func showWindow() {
        
        let viewController : CustomController = CustomController.init()
        
        self.popupWindow = SDKWindow.init(rootController: viewController)
        self.popupWindow?.show()
    }

public func hide() {
        
        self.popupWindow?.hide()
        
    }

}

When hide method is called, CustomViewController Deinit is not called. But when

public func hide() {
            
            self.popupWindow?.hide()
            self.popupWindow = nil
            
        }

is performed, the CustomViewController deinit is called and UIWindow instance is also deallocated in memory.

0

There are 0 best solutions below