I think you’re making things needlessly complicated in your mental concept around an unfortunate wording in the documentation (which should not have said “[unless you have] called g_object_ref()
on the mark” in those words, but probably rather “unless you are holding a reference to the mark” or something).
As Emmanuele says, it’s a very straightforward instance of plain reference counting, nothing bizarre or convoluted here: add_mark()
adds a reference, and delete_mark()
drops it. You mention create_mark()
seemingly as a source of confusion on the reference count after a delete_mark()
call, but when you see create_mark()
doesn’t take an object, it’s not far fetched to accept it not adding an extra reference (while internally it could just release the reference after having called add_mark()
).
The “expected” handing on your side when using add_mark()
is for you to release your reference to it when you don’t want to hold one yourself, and let the buffer keep (or not) its own reference as it needs.
So basically, either:
- you have a mark you want to reuse and keep a long-lived reference to it,
- you want to create a mark and “hand it over” to the buffer, and then you call
unref()
right after add_mark()
so the buffer has the only reference to it.
In the first case, remove_mark()
will not destroy the object as you still have a reference to it, otherwise you already released the reference and it’ll be dropping the last one, thus collecting the object.
GtkTextMark *mark = gtk_text_mark_new(NULL, FALSE);
while (…) {
gtk_text_buffer_add_mark(buffer, mark, &iter); // adds a reference to mark
// …
gtk_text_buffer_delete_mark(buffer, mark); // removes a reference to mark
}
g_object_unref(mark); // releases the last reference created by new()
/* or */
while (…) {
GtkTextMark *mark = gtk_text_buffer_create_mark(buffer, NULL, &iter, FALSE); // creates a mark and own the reference to it, just returning an unowned pointer (transfer: none)
// …
gtk_text_buffer_delete_mark(buffer, mark); // releases the last reference to the mark
}
The problem I think only arises from the documentation mentioning ref()
, which is only really relevant if the mark was created with create_mark()
. This said, it’s often more convenient just use create_mark()
than GtkTextMark::new()
followed by add_mark()
(and ref the retrurned mark if you really want to hold on to it).