Ah, right. So, I'll channel Klonan: always check .valid
on anything that has it (which is ~ every LUA API object) before using it. No matter what.
Which means that you need to check it both when our code is started by, eg, an event, or an input, or whatever, and also after any call to the API, since that can trigger events, and other mods can destroy things without raising an event. Checking .valid is effectively free, though, so you can safely do it too much and it won't even show up as a blip on the performance radar.
I strongly advise doing this preemptively if you don't want an unending string of these reports until you finally end up checking everywhere anyway, one bug at a time. :)