为什么我在执行远程获取时在 libgit2 中命中断言

why do I hit assert in libgit2 when performing remote fetch

我正在尝试实现与 libgit2 的合并,在执行 remote_fetch 或 create_annotated_commit 后跟 git_repository_free[=12 时,我总是遇到这个断言=]

但是我不知道我做错了什么?并且没有生成 libgit 错误。而且我也不明白这个assert的意思

一个典型的调用栈如下:

2  __GI_abort              abort.c       90  0x7f35da541f5d 
3  __assert_fail_base      assert.c      92  0x7f35da537f17 
4  __GI___assert_fail      assert.c      101 0x7f35da537fc2 
5  git_mwindow_put_pack    mwindow.c     106 0x7f35db69725f 
6  pack_backend__free      odb_pack.c    565 0x7f35db6a3dda 
7  odb_free                odb.c         679 0x7f35db69d5c4 
8  git_odb_free            odb.c         696 0x7f35db69d67c 
9  set_odb                 repository.c  95  0x7f35db6d40c3 
10 git_repository__cleanup repository.c  150 0x7f35db6d42a3 
11 git_repository_free     repository.c  161 0x7f35db6d42da 
12 GitEngine::~GitEngine   GitEngine.cpp 268 0x556a9e4f067d 
13 main                    main.cpp      24  0x556a9e4eed8e 

要重现这个问题,我只需要这样做:

    git_remote *remote;
    int err = git_remote_create(&remote, m_repo, "remotefetch", "./repoD/");
    git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
    if (err) {
        qDebug() << "error";
    }
    err = git_remote_fetch(remote, nullptr, &fetch_opts, nullptr);
    if (err) {
        qDebug() << "error";
    }

    if (m_repo) //this is in the destructor 
    git_repository_free(m_repo);

如果我用 git_remote_create_anonymous 替换 git_remote_create,问题就会消失。

但是,带有 git_remote_create_anonymous 的事件,如果我稍后在 git_repository_free 之前调用 git_annotated_commit_from_fetchhead ,我将再次使用相同的断言。我不知道为什么?我用命令行检查了 git 存储库,合并似乎正确执行。

当库本身处理了其内部全局状态时,有时会发生这种情况,该状态使用 git_libgit2_init 配置并使用 git_libgit2_shutdown 销毁。

在调用任何 libgit2 函数之前,您必须 调用 git_libgit2_init。完成后,您应该 调用git_libgit2_shutdown。 (虽然如果您只是关闭您的应用程序,则不一定要这样做,任何打开的资源都会被操作系统关闭。)

但是,您不得 在调用 git_libgit2_shutdown 之后调用任何 libgit2 函数。这样做会尝试引用已释放的资源。您的堆栈跟踪表明您在调用 git_repository_free.

之前调用 git_libgit2_shutdown

如果您颠倒这些调用的顺序,那么事情应该没问题。