sm: improve resource cleanup