安全释放 libgit2 对象

Safely free libgit2 objects

我试图了解何时释放某些 libgit2 对象是安全的(我正在为垃圾收集语言编写绑定,并且需要跟踪哪些对象需要保留对其他对象的引用)。

git_repository_free 状态的文档:

Note that after a repository is free'd, all the objects it has spawned will still exist until they are manually closed by the user with git_object_free, but accessing any of the attributes of an object without a backing repository will result in undefined behavior

这里的"attributes"是什么意思?它似乎没有提到 "git attributes",即 git_attr_* functions,因为这些是存储库本身的属性,而不是对象。

  1. 这是否意味着我不应该对该存储库中的任何对象调用 git_object_free 以外的任何方法?

  2. 这是否仅适用于 "git objects"(即标记、提交、树和 blob)?例如,我可以安全地使用调用 git_repository_free 后通过 git_repository_config 获得的 git_config 吗?

  3. 还有其他我需要注意的情况吗?

例如git_repository_free man page does not mention "git attributes" (nothing to do with .gitattributes

... accessing any of the attributes of an object

这是更通用的 OOP term attribute

Attributes

These store information about the object. In the example above we store the fuel and maxSpeed.
The attributes are attached to the classes, and if there are several instances (objects) of the classes then each will store its own version of these variables.

作为 by the OP Simon Byrne, libgit2 attributes are not exposed directly. Still if there are accessor functions 这些属性的 return 值,如果释放所有回购对象,您仍然会得到不好的结果。

释放该对象占用的内存后访问对象属性将导致不可预知的结果。

您应该拥有另一个 Git 存储库(“后备存储库”,您刚刚释放的存储库的克隆)的有效句柄,以便再次访问这些属性。

所以:

  1. 否:仅适用于 git-存储库指针,如 tests/object/lookup.c 所示。只要另一个对象不保留对刚刚释放的 git_repository 的引用,其他对象就可以。例如,*commit 只是一个字符串:您仍然可以使用它(但与另一个 git 存储库一起使用)
  2. 我看不到