shadowmage45 / TexturesUnlimited

KSP Shader, Texture, and Modeling Utilities
GNU General Public License v3.0
29 stars 17 forks source link

NRE From TexturesUnlimitedDebug #81

Closed marr75 closed 4 years ago

marr75 commented 5 years ago

TexturesUnlimitedDebug.Awake will (as far as I can tell) always throw an NRE if TUGameSettings.Debug is false. Your code comment is that you're removing the button because you know it's not null but debug isn't true and that just isn't so.

Your conditionals are debugAppButton == null && debug and debugAppButton != null && debug, ultimately, in the most common case where debug is always false, you're just testing the value of debug so your assumption that the button isn't null is false.

Seems simpler to keep away from that bug by doing:

if (debug) {
    if (debugAppButton == null) {
        tex = GameDatabase.Instance.GetTexture("Squad/PartList/SimpleIcons/RDIcon_fuelSystems-highPerformance", false);
        debugAppButton = ApplicationLauncher.Instance.AddModApplication(
            debugGuiEnable, debugGuiDisable, null, null, null, null,
            ApplicationLauncher.AppScenes.FLIGHT | ApplicationLauncher.AppScenes.SPH | ApplicationLauncher.AppScenes.VAB, tex
        );
    }
    else {
        // reset callback refs to the ones from THIS instance of the KSPAddon (old refs were stale, pointing to methods for a deleted class instance)
        debugAppButton.onTrue = debugGuiEnable;
        debugAppButton.onFalse = debugGuiDisable;
    }
}
else if (debugAppButton != null) {
    ApplicationLauncher.Instance.RemoveModApplication(debugAppButton);
}

I've remedied it in my local copy of TU using dnSpy and I've got too many forks and PRs floating around the KSP world right now but here's the issue and that code will fix it.

shadowmage45 commented 5 years ago

Thanks for spotting it and providing a potential solution.

I should maybe test my releases in non debug mode once in awhile, eh? :)