Files
sbox-public/engine/Sandbox.Tools/Utility
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
..
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2025-11-24 09:05:18 +00:00
2026-02-03 18:29:40 +00:00
2025-11-24 09:05:18 +00:00