fix memory leak (and crash under debugging) by not trying to unload textures on a non-GL thread

This commit is contained in:
Colin McMillen 2023-07-28 12:19:15 -04:00
parent 75b80507bd
commit 3a0060fc9d

View File

@ -212,10 +212,10 @@ public class Photo {
image = tmp; image = tmp;
} }
public async void UnloadAsync() { public async void Unload() {
Loaded = false; Loaded = false;
if (texture != placeholder) { if (texture != placeholder) {
await Task.Run( () => { texture.Dispose(); }); texture.Dispose();
texture = placeholder; texture = placeholder;
} }
} }
@ -812,7 +812,7 @@ public class Game : GameWindow {
int minLoadedImage = Math.Max(0, photoIndex - 20); int minLoadedImage = Math.Max(0, photoIndex - 20);
int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1); int maxLoadedImage = Math.Min(photoIndex + 20, photos.Count - 1);
// First, unload images that haven't been touched in a while. // First, unload images that haven't been touched in a while.
// FIXME: also cancel any of these if they still have an in-progress loading task -- I suspect this is the source of a memory leak. // FIXME: also cancel any of these if they still have an in-progress loading task?
// FIXME: keep around thumbnail-sized textures? // FIXME: keep around thumbnail-sized textures?
while (loadedImages.Count > 60) { while (loadedImages.Count > 60) {
long earliestTime = long.MaxValue; long earliestTime = long.MaxValue;
@ -825,7 +825,8 @@ public class Game : GameWindow {
} }
if (earliest != null) { if (earliest != null) {
Console.WriteLine($"loadedImages.Count: {loadedImages.Count}, evicting {earliest.Filename} @ {earliestTime}"); Console.WriteLine($"loadedImages.Count: {loadedImages.Count}, evicting {earliest.Filename} @ {earliestTime}");
earliest.UnloadAsync(); // TODO: we have to free textures on the GL thread, but could we do that async'ly to keep the UI responsive?
earliest.Unload();
loadedImages.Remove(earliest); loadedImages.Remove(earliest);
} }
} }