[fix] memory leak when the layer is disconnected

This situation may happen whenever the client is disconnected while sending a message