bugfix: potential misadressing on session close

This can happen if also a write was outstanding, a quite unusable
situation. In that case, already freed memory was being accessed.