Commit Graph

7 Commits

Author SHA1 Message Date
Lorenz Junglas
54932b6725 (Shutdown) Leak Fixes (#4242)
* Remove unnecessary static singletons in MainMenu code

* Empty SceneWorld delete queues during shutdown

* Dresser cancel async load operation on destroy

* Use reflection to null references to static native resources on shutdown

This way we don't  have to remember doing this manually.

* Fix SoundOcclusionSystem using static lists to reference resources

* Sound System: Use weak references to refer to scenes

* Cleanup static logging listeners that kept strong refs to panels

* UISystem Cleanup, make sure all panel/stylesheet refs are released

* RenderTarget and RenderAttributes shutdown cleanup

* Rework AvatarLoader, ThumbLoader & HTTPImageLoader Cache

First try to go through ResourceLibrary.WeakIndex which might already hold the texture.

If there is no hit, go through a second cache that caches HTTP & Steam API response bytes instead of textures.
We want to cache the response bytes rather than the actual Texture, so stuff cached sits in RAM not VRAM.
Before avatars and thumbs would reside in VRAM.

* Fix rendertarget leak in CommandList.Attr.SetValue

GetDepthTarget() / GetColorTarget() return a new strong handle (ref count +1).
We need to DestroyStrongHandle()  that ref. So handles don't leak.

* Call EngineLoop.DrainFrameEndDisposables before shutdown

* NativeResourceCache now report leaks on shutdown

* Override Resource.Destroy for native resources, kill stronghandles

* Deregister SceneWorld from SceneWorld.All during destruction

* Ensure async texture loading cancels on shutdown

* SkinnedModelRender bonemergetarget deregister from target OnDisabled

* Clear shaderMaterials cache during shutdown

* Refactor Shutdown code

Mostly renaming methods from Clear() -> Shutdown()
Adding separate GlobalContext.Shutdown function (more aggressive than GlobalContext.Reset).
Clear some static input state.

* Deregister surfaces from Surface.All in OnDestroy

* RunAllStaticConstructors when loading a mount

* Advanced managed resource leak tracker enabled via `resource_leak_tracking 1`

Works by never pruning the WeakTable in NativeResourceCache.
So we can check for all resources if they are still being held on to and log a callstack.
2026-03-09 17:02:27 +01:00
Conna Wiles
6262249de1 Time/RealTime.NowDouble (#4090) 2026-02-22 14:56:54 +00:00
Lorenz Junglas
3a5e880727 Fix ResourceIndex holding strong references to native resources (#4044)
Fixes https://github.com/Facepunch/sbox/issues/4039

`ResourceIndex` was keeping every native resource alive forever. Materials, models, textures — once loaded, they could never be GC'd. This completely defeated `NativeResourceCache` which was designed around weak references.

The fix: native resources now register into a `WeakIndex` instead of `ResourceIndex`. Networking still works (resources can be looked up by `ResourceId`), but GC can reclaim them when nothing else holds a reference.

Also migrated `Texture` to use `NativeResourceCache` for dedup (matching what Model/Material already do) and removed the old `Texture.Loaded` / `Texture.LoadedByPointer` dictionaries that were either redundant or dead code.

### Summary

- Added `WeakIndex` to `ResourceSystem` — weak reference lookup alongside the strong `ResourceIndex`
- Renamed `SetIdFromResourcePath` → `RegisterWeakResourceId` to make intent clear
- `NativeResourceCache`: fixed a TOCTOU race on `WeakReference.Target`, added `Remove()` for explicit disposal, reduced expiration 30s → 5s
- `Texture.FromNative`: now goes through `NativeResourceCache`, handles disposed textures being returned from cache (evicts + recreates)

### overlay_resources

Added a debug overlay to help track this stuff going forward. Shows live counts for `ResourceIndex`, `WeakIndex`, and `NativeResourceCache` grouped by type, with all-time max tracking.

https://files.facepunch.com/lolleko/2026/February/16_17-20-CraftyHoopoe.png
2026-02-16 17:20:48 +01:00
Matt Stevens
8f859f0d57 Revert "Run Steam Callbacks in Network Thread (#3984)"
This reverts commit 848580f804.
2026-02-04 15:36:30 +00:00
Conna Wiles
848580f804 Run Steam Callbacks in Network Thread (#3984) 2026-02-03 15:40:34 +00:00
Lorenz Junglas
6808d8768e Shutdown fixes (#3553)
* Stop generating solutions via -test flag add -generatesolution

* Add TestAppSystem remove Application.InitUnitTest

Avoids some hacks and also makes sure our tests are as close to a real AppSystem as possible.

* Add shutdown unit test

shuts down an re-inits the engine

* Properly dispose native resources hold by managed during shutdown

Should fix a bunch of crashes

* Fix filesystem and networking tests

* StandaloneTest does proper Game Close

* Make sure package tests clean up properly

* Make sure menu scene and resources are released on shutdown

* Report leaked scenes on shutdown

* Ensure DestroyImmediate is not used on scenes

* Fix unmounting in unit tests not clearing native refs

* Force destroy native resource on ResourceLib Clear
2025-12-08 15:55:11 +01:00
s&box team
71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00