mcpiroman / UnityNativeTool

Allows to unload native plugins in Unity3d editor
MIT License
184 stars 19 forks source link

Consider changing OnApplicationQuit to OnDisable #8

Closed Calandiel closed 5 years ago

Calandiel commented 5 years ago

Script execution order has no impact on OnApplicationQuit. As such, cleaning up work done by a dll after leaving play mode is a bit clunky. You can't do it in OnApplicationQuit on a different monobehaviour because the dll may already be unloaded by that point. Changing OnApplicationQuit to OnDisable inside DllManipulatorScript would give a clear window of opportunity for any fixes or logging before unloading.

mcpiroman commented 5 years ago

I recommend setting execution order of this script to be the lowest so that it can wire things up (and potentially load dlls) before everything else is run. This is not meant to affect OnApplicationQuit.

However, you're correct about the de facto race condition that happens between OnApplicationQuit on this and other scripts. If OnDisable always runs after OnApplicationQuit (doesn't it?), then this really would be a good change. That still wouldn't be totally correct as someone might access a dll in OnDisable too, but definitely better. Also, maybe OnDestroy would be a better choice? It should run after OnDisable afaik. Anyways, thanks for suggestion.

Calandiel commented 5 years ago

Yeah, OnDestroy would be an even better place to call it