Script node lifetime fix

This fixes a nasty circular reference problem which could keep ScriptNodes alive for ever. This had a knock-on effect preventing proper shutdown, resulting in the crashes described in #397.