For ENG-5865, more fixes for memory ownership