AB4D Forum
Avalonia TabControl causes MainSceneView to be disposed - Printable Version

+- AB4D Forum (https://forum.ab4d.com)
+-- Forum: Products Forums (https://forum.ab4d.com/forumdisplay.php?fid=4)
+--- Forum: Ab4d.SharpEngine (https://forum.ab4d.com/forumdisplay.php?fid=12)
+--- Thread: Avalonia TabControl causes MainSceneView to be disposed (/showthread.php?tid=4505)



Avalonia TabControl causes MainSceneView to be disposed - Oplopanax - 02-07-2026

I am having an unexpected behaviour with the Avalonia TabControl.

The Scene is initialized when the control is first shown, but if you tab to another control and come back the Scene is disposed. When you try to move the camera, the following exception is thrown:

Code:
  at Ab4d.SharpEngine.SceneNodes.SceneNode.Update()
  at Ab4d.SharpEngine.Scene.Update(Boolean forceUpdateAll)
  at Ab4d.SharpEngine.Scene.GetClosestHitObject(Ray ray, SceneNode rootSceneNode, HitTestOptions customHitTestOptions)
  at Ab4d.SharpEngine.SceneView.GetClosestHitObject(Ray ray, HitTestOptions customHitTestOptions)
  at Ab4d.SharpEngine.Utilities.CameraController.GetRotationCenterPositionFromPointerPosition(Vector2 pointerPosition, Boolean calculatePositionWhenNoObjectIsHit)
  at Ab4d.SharpEngine.Utilities.CameraController.UpdateRotationCenterPosition(Vector2 pointerPosition, Boolean calculatePositionWhenNoObjectIsHit)
  at Ab4d.SharpEngine.Utilities.CameraController.StartCameraRotation(Vector2 pointerPosition)
  at Ab4d.SharpEngine.Utilities.ManualPointerCameraController.StartPointerProcessing(Vector2 pointerPosition, PointerButtons pressedButtons, KeyboardModifiers keyboardModifiers)
  at Ab4d.SharpEngine.Utilities.ManualPointerCameraController.ProcessPointerMoved(Vector2 pointerPosition, PointerButtons pressedPointerButtons, KeyboardModifiers keyboardModifiers)
  at Ab4d.SharpEngine.AvaloniaUI.PointerCameraController.♔(Object ♔, PointerEventArgs ♔)
  at Avalonia.Interactivity.Interactive.<AddHandler>g__InvokeAdapter|8_0[TEventArgs](Delegate baseHandler, Object sender, RoutedEventArgs args)
  at Avalonia.Interactivity.Interactive.<>c__8`1.<AddHandler>b__8_1(Delegate baseHandler, Object sender, RoutedEventArgs args)
  at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)
  at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)
  at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)
  at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e)
  at Avalonia.Controls.TopLevel.<>c.<HandleInput>b__150_0(Object state)
  at Avalonia.Threading.Dispatcher.Send(SendOrPostCallback action, Object arg, Nullable`1 priority)
  at Avalonia.Controls.TopLevel.HandleInput(RawInputEventArgs e)
  at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
  at Avalonia.Win32.WindowImpl.WndProcMessageHandler(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
  at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
  at Avalonia.Win32.Win32DispatcherImpl.RunLoop(CancellationToken cancellationToken)
  at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
  at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
  at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
  at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.StartCore(String[] args)
  at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
  at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
  at AB4D.TabControlIssue.Program.Main(String[] args) in D:\Development\Ab4d.SharpEngine.Samples-main\TabControlSample\Program.cs:line 12

I created a project at GitHub to illustrate the issue

https://github.com/MichaelFCoyle/AB4D.TabControlIssue

In the main application I am working with a different exception is thrown because I am adding points to the root node but basically the MainSceneView, Scene, SceneView and RootNode are all disposed.

Is there guidance where I can re-create the scene in code every time the TabControl is shown?


RE: Avalonia TabControl causes MainSceneView to be disposed - abenedik - 02-09-2026

Thank you for preparing a sample app that reproduces the issue. This way I was very quickly able to reproduce the problem and find the fix.

In your app the SharpEngineSceneView control is being disposed because in the _3DControl you have the following line:

this.Unloaded += (sender, args) => MainSceneView.Dispose();

This means that if the  _3DControl is hosted in a TabControl and if the tab that shows the  _3DControl is changed, then the SharpEngineSceneView is disposed. So the next time the tab is opened, you have a disposed  SharpEngineSceneView that cannot be used anymore.

If you just comment out this line, the app will start working.

But a better option is to provide a Dispose method in the  _3DControl. There you dipose the MainSceneView. That Dispose method can be called the parent MainWindow is Unloaded - so when you close the window, you can dispose the SharpEngineSceneView because it will not be used anymore.