X-Hax / sa_tools

Sonic Adventure Toolset
http://info.sonicretro.org/SA_Tools
103 stars 24 forks source link

Memory leak in SA Tools #109

Closed PiKeyAr closed 4 years ago

PiKeyAr commented 4 years ago

I noticed that SAMDL, SADXLVL2, SALVL and Texture Editor (possibly other tools too) don't clear memory when you load a new file. Memory usage keeps growing with each new file, and the increase is usually proportional to the size of the file being loaded. SADXLVL2 in particular can grow to over 1GB pretty quickly if you load multiple levels without restarting the program, which makes it slow and unresponsive. With SAMDL and SALVL the leak also happens if you load stuff from a binary instead of a level/model file. Selecting "Clear level" in SADXLVL2 and SALVL doesn't seem to clean up either.

PiKeyAr commented 4 years ago

I've tested memory usage after the latest fixes using a memory profiler, and the leak appears to be gone. SALVL is good now, SADXLVL2 still had a leak related to path data but I fixed it by cleaning out PointHelpers before loading a stage. Memory usage is good now after reloading a stage in level editors, and Texture Editor and SAMDL didn't have any leaks in the first place.

michael-fadely commented 4 years ago

fwiw

It's not likely that we've run into a GC bug, and that memory should have been automatically freed when the GC runs, so unless it was manually managed memory somewhere, it should have been fine.

That said, I recall there being some memory leaks in SharpDX.

MainMemory commented 4 years ago

Every Item registers an event handler for the EditorSelectionManager's SelectionChanged event, but previously, the handlers weren't ever being unregistered, so the EditorSelectionManager was keeping references to every Item that was created and never releasing them, causing a memory leak. Now, we unregister the event handler in the Item's Delete function, as well as re-initializing the EditorSelectionManager upon loading a new level, so everything gets cleaned up as it should.