For ENG-5865, fix double free issues and unify cleaning for managed and unmanaged bytebuffers