Possible fix for lua crash #5269

The changes here fix some disposing issues but this crash is very hard to reproduce so I can't really tell if it has been fixed.