diff --git a/include/libgit4cpp/Remote.h b/include/libgit4cpp/Remote.h index 8947c23..64b0293 100644 --- a/include/libgit4cpp/Remote.h +++ b/include/libgit4cpp/Remote.h @@ -72,6 +72,19 @@ class Remote std::vector list_references(); private: + // Keeps libgit2 initialized for as long as the remote exists, because the remote may + // outlive its Repository. Declared before remote_ so that it is destroyed after it. + struct LibGitInitGuard + { + LibGitInitGuard() { git_libgit2_init(); } + LibGitInitGuard(LibGitInitGuard&&) { git_libgit2_init(); } + LibGitInitGuard(const LibGitInitGuard&) = delete; + LibGitInitGuard& operator=(LibGitInitGuard&&) = default; + LibGitInitGuard& operator=(const LibGitInitGuard&) = delete; + ~LibGitInitGuard() { git_libgit2_shutdown(); } + }; + + LibGitInitGuard init_guard_; LibGitRemote remote_{ nullptr, git_remote_free }; };