Safely free libgit2 objects

95 views Asked by At

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.

  1. Does this mean that I should not call any method other than git_object_free on any object from that repository?

  2. 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 via git_repository_config after calling git_repository_free?

  3. Are there any other cases that I need to be aware of?

1

There are 1 answers

8
VonC On BEST ANSWER

The git_repository_free man page does not mention "git attributes" (nothing to do with .gitattributes for instance)

... accessing any of the attributes of an object

This is the more generic 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.

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:

  1. Yes
  2. No: that applies only to a git-repository pointer, as shown in tests/object/lookup.c. As long as another another does not keep a reference to the git_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)
  3. Not that I can see