I'm trying to understand when it is safe to free certain libgit2 objects (I'm writing bindings for a garbage-collected language, and need to keep track of which objects need to keep references to other objects).
The docs for git_repository_free
state:
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
What does "attributes" mean here? It doesn't seem to refer to the "git attributes", i.e. the git_attr_*
functions, as these are properties of the repository itself, not objects.
Does this mean that I should not call any method other than
git_object_free
on any object from that repository?Does this only apply to "git objects" (i.e. tags, commits, trees and blobs)? For example, can I safely use a
git_config
that was obtained viagit_repository_config
after callinggit_repository_free
?Are there any other cases that I need to be aware of?
The
git_repository_free
man page does not mention "git attributes" (nothing to do with.gitattributes
for instance)This is the more generic OOP term attribute
As commented by the OP Simon Byrne, libgit2 attributes are not exposed directly. Still if there are accessor functions which return values from those attributes, you would still get bad results if the all repo object was freed.
Accessing an object attribute after freeing the memory occupied by that object will lead to unpredictable result.
You should have a valid handle to another Git repo (a "backing repository", a clone of the repository you just freed) in order to access those attributes again.
So:
tests/object/lookup.c
. As long as another another does not keep a reference to thegit_repository
that just got freed, that other object is fine. For instance, a*commit
is just a string: you can still use it (but with another git repo)