Crash occur with "Attempting to attach window to an invalidated scene" message

Hello. Recently, our app has been experiencing crashes with the message 'Attempting to attach window to an invalidated scene' when creating a UIWindow.

Our code stores the UIWindowScene provided in the scene(:willConnectTo:options:) function in a global variable and does not change the set scene until the scene(:willConnectTo:options:) function is called again. Additionally, we do not perform any actions related to the disconnect event.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    hudManager.setup(windowScene: windowScene)
    // ...
}

func sceneDidDisconnect(_ scene: UIScene) {
    // do nothing
}

In all crash logs, the activationState of the WindowScene is "unattached", so I initially thought that creating a UIWindow with a scene in the 'unattached' state should be avoided.

However, in the scene(_:willConnectTo:options:) function, the scene's state is also 'unattached', yet the UIWindow is created successfully here, which makes me think that deciding whether to create a window based on the activationState is incorrect.

I did find that trying to create a UIWindow with a scene after it has been disconnected causes a crash.

func sceneDidDisconnect(_ scene: UIScene) {
    // Crash occur here and scene's state is `unattached`
    let window = UIWindow(windowScene: scene as! UIWindowScene)
}

If the activationState alone cannot be used to determine the validity of a scene, is there another way to check the validity of a Scene? Thank you

Answered by Frameworks Engineer in 827755022

Our code stores the UIWindowScene provided in the scene(:willConnectTo:options:) function in a global variable and does not change the set scene until the scene(:willConnectTo:options:) function is called again. Additionally, we do not perform any actions related to the disconnect event.

When the UIScene instance is disconnected, it is completely invalid and ready for deallocation. It will not be used again. Holding onto the scene past deallocate is not valid.

In all crash logs, the activationState of the WindowScene is "unattached", so I initially thought that creating a UIWindow with a scene in the 'unattached' state should be avoided.

No, when the scene is about to connect, it is not yet connected and its state will be unattached.

If the activationState alone cannot be used to determine the validity of a scene, is there another way to check the validity of a Scene?

The scene instance is valid from when you are notified about it being vended to you by the system in scene(_:willConnectTo:options:) until you receive sceneDidDisconnect. You don't need to manage or really think of its validity outside those two delegate callbacks.

Accepted Answer

Our code stores the UIWindowScene provided in the scene(:willConnectTo:options:) function in a global variable and does not change the set scene until the scene(:willConnectTo:options:) function is called again. Additionally, we do not perform any actions related to the disconnect event.

When the UIScene instance is disconnected, it is completely invalid and ready for deallocation. It will not be used again. Holding onto the scene past deallocate is not valid.

In all crash logs, the activationState of the WindowScene is "unattached", so I initially thought that creating a UIWindow with a scene in the 'unattached' state should be avoided.

No, when the scene is about to connect, it is not yet connected and its state will be unattached.

If the activationState alone cannot be used to determine the validity of a scene, is there another way to check the validity of a Scene?

The scene instance is valid from when you are notified about it being vended to you by the system in scene(_:willConnectTo:options:) until you receive sceneDidDisconnect. You don't need to manage or really think of its validity outside those two delegate callbacks.

Crash occur with "Attempting to attach window to an invalidated scene" message
 
 
Q