Skip to content

Commit 23a6a47

Browse files
committed
fix: keep lib alive until gc
1 parent 91819fb commit 23a6a47

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ffi.cdef [[
2323
typedef struct { const char *message; int klass; } git_error;
2424

2525
const git_error *git_error_last(void);
26-
int git_libgit2_init(void);
26+
int git_libgit2_init(void);
27+
void git_libgit2_shutdown(void);
2728

2829
int git_repository_open(git_repository **out, const char *path);
2930
int git_repository_init(git_repository **out, const char *path, unsigned is_bare);
@@ -278,4 +279,8 @@ function git2.init(path, bare)
278279
return Repo.new(rp[0])
279280
end
280281

282+
-- Call git_libgit2_shutdown when the module is GC'd to avoid a segfault on DLL unload.
283+
-- The closure keeps `lib` alive until after shutdown runs.
284+
git2._gc = ffi.gc(ffi.new("char[1]"), function() lib.git_libgit2_shutdown() end)
285+
281286
return git2

0 commit comments

Comments
 (0)