BUG: check validity of node before operating on it

Fixes a crash during scene shutdown.

git-svn-id: http://svn.slicer.org/Slicer4/trunk@23034 3bd1e089-480b-0410-8dfb-8563597acbee