Make sure child states are deleted first.

The previous code ran the destructors of the parent state first, which
caused issues. The child states should always be deleted first.