Search code examples
gitrepositorydisposeusing-statementlibgit2sharp

should libgit2sharp Repository be explicitly disposed?


Should a libgit2sharp Repository object be explicitly disposed ?

If clean-up is not always performed via Repository.Dispose() , what are the possible negative consequences ? Could a non-GC'd Repository object retain locks on various .git files ?

Is it best to wrap Repository object in using statement ?

var localRepo = new Repository("{local repo path}", new RepositoryOptions());
using (localRepo) {
    // checkout
    // etc.
}

Solution

  • Disposing a Repository is recommended practice.

    Indeed, this type holds pointers to non managed resources (native memory, indirect pointers to file handles under the .git directory, ...).

    Dispose() will make sure that those resources are properly freed/released.

    However, some safety net has been implemented would the caller forget to do so. the Repository type defines a finalizer which will take care of this.

    Keep in mind that the moment this finalizer will run isn't predictable (as stated by the documentation "before it is reclaimed by garbage collection").

    Thus, the recommendation to control by yourself the disposal of your repository.