Session locking, reference counting and freeing