ApacheTech-VintageStory-Mods / ApacheTech.VintageMods.CampaignCartographer

Mod for Vintage Story: Adds multiple Cartography related features to the game, such as custom waypoint icons, GPS, auto waypoint markers, and more.
7 stars 4 forks source link

[BUG] Leaking Textures in PlayerPins Map Markers. #22

Closed ApacheTech closed 2 years ago

ApacheTech commented 2 years ago
24.2.2022 20:36:05 [Warning] Texture with texture id 420 is leaking memory, missing call to Dispose. Allocated at    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at ApacheTech.VintageMods.CampaignCartographer.Features.PlayerPins.Patches.PlayerMapLayerPatches.LoadTexture(Color colour, Int32 scale)
   at ApacheTech.VintageMods.CampaignCartographer.Features.PlayerPins.Patches.PlayerMapLayerPatches.AddPlayerToMap(IPlayer player)
   at Vintagestory.API.Client.PlayerEventDelegate.Invoke(IClientPlayer byPlayer)
   at Vintagestory.Client.NoObf.ClientSystemEntities.OnEntitySpawnOrLoaded(Entity entity, Boolean loaded)
   at Vintagestory.Client.NoObf.ClientEventManager.TriggerEntityLoaded(Entity entity)
   at Vintagestory.Client.NoObf.ClientSystemEntities.HandleEntitiesPacket(_vHBGzImmzojsCBb4Qveu6vbHGjC serverpacket)
   at Vintagestory.Client.NoObf.ClientMain.ExecuteMainThreadTasks(Single deltaTime)
   at _shKd59ll3QCLo970uNLb4PQQzpO._rLTCCIQUh1fAy5ylbbNZOwGtuCL(Single )
   at _tFdr1uT5zfyUkzNSkA3ToX5ljzA._W214eNLFnAzGPzBQUUjctRZn5C(Single )
   at _tFdr1uT5zfyUkzNSkA3ToX5ljzA._G5t0g6xoyNnwYQD1Na16N3BI01d(Single )
   at Vintagestory.Client.NoObf.ClientPlatformWindows.window_RenderFrame(Object sender, FrameEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at OpenTK.GameWindow.RaiseRenderFrame(Double elapsed, Double& timestamp) in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 479
   at OpenTK.GameWindow.DispatchRenderFrame() in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 454
   at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second) in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 375
   at _NxLBujraSWHVxPxGtdSuefM8ihcA._trQCfDx6Bhpsp3kfLOJy9Wami1D(_Fcxbj1ZPAR3EYINauNuPOcAF6oib , String[] )
   at _a7XtyCAKDX5vZfnNApsGQEUBZQT._trQCfDx6Bhpsp3kfLOJy9Wami1D(ThreadStart )
   at _NxLBujraSWHVxPxGtdSuefM8ihcA._98A9SKkDc5ieBKeYFRZZzoC11Qr(String[] ).
24.2.2022 20:36:05 [Warning] Texture with texture id 1091 is leaking memory, missing call to Dispose. Allocated at    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at ApacheTech.VintageMods.CampaignCartographer.Features.PlayerPins.Patches.PlayerMapLayerPatches.LoadTexture(Color colour, Int32 scale)
   at ApacheTech.VintageMods.CampaignCartographer.Features.PlayerPins.Patches.PlayerMapLayerPatches.AddPlayerToMap(IPlayer player)
   at Vintagestory.API.Client.PlayerEventDelegate.Invoke(IClientPlayer byPlayer)
   at Vintagestory.Client.NoObf.ClientSystemEntities.OnEntitySpawnOrLoaded(Entity entity, Boolean loaded)
   at Vintagestory.Client.NoObf.ClientEventManager.TriggerEntityLoaded(Entity entity)
   at Vintagestory.Client.NoObf.ClientSystemEntities.HandleEntitiesPacket(_vHBGzImmzojsCBb4Qveu6vbHGjC serverpacket)
   at Vintagestory.Client.NoObf.ClientMain.ExecuteMainThreadTasks(Single deltaTime)
   at _shKd59ll3QCLo970uNLb4PQQzpO._rLTCCIQUh1fAy5ylbbNZOwGtuCL(Single )
   at _tFdr1uT5zfyUkzNSkA3ToX5ljzA._W214eNLFnAzGPzBQUUjctRZn5C(Single )
   at _tFdr1uT5zfyUkzNSkA3ToX5ljzA._G5t0g6xoyNnwYQD1Na16N3BI01d(Single )
   at Vintagestory.Client.NoObf.ClientPlatformWindows.window_RenderFrame(Object sender, FrameEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at OpenTK.GameWindow.RaiseRenderFrame(Double elapsed, Double& timestamp) in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 479
   at OpenTK.GameWindow.DispatchRenderFrame() in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 454
   at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second) in C:\Users\Nexrem\Desktop\transfer\opentk\src\OpenTK\GameWindow.cs:line 375
   at _NxLBujraSWHVxPxGtdSuefM8ihcA._trQCfDx6Bhpsp3kfLOJy9Wami1D(_Fcxbj1ZPAR3EYINauNuPOcAF6oib , String[] )
   at _a7XtyCAKDX5vZfnNApsGQEUBZQT._trQCfDx6Bhpsp3kfLOJy9Wami1D(ThreadStart )
   at _NxLBujraSWHVxPxGtdSuefM8ihcA._98A9SKkDc5ieBKeYFRZZzoC11Qr(String[] ).
ApacheTech commented 2 years ago

Happens after:

24.2.2022 20:36:04 [Notification] Exiting current game to main menu, reason: leave world button pressed

I am already purging the PlayerPins Dictionary when the class is disposed. This could be a race condition issue. As a safety measure, I've added a loop that explicitly disposes the texture, within each MapComponent, prior to purging the dictionary, and added a finaliser to the class, to hopefully degrade gracefully as the static class loses scope, without the Dispose method being called.

It's likely that within the degredation chain, my class was disposed before the vanilla classes, so as it went out of scope, the dispose method was never explicitly called. The finaliser should solve that issue... maybe.

One final measure has been to add an event handler to manually dispose the static resources as the player leaves the world.

I think, with these measures in place, I should have catered for each eventuality. However, it will be an on-going learning experience to find how, when, and where to dispose of static resources.