If I have an object atype obj where atype is defined like typedef struct myType {...} * atype, is there any way I can get all the references to obj, or at least how many there are?
Something like:
atype obj;
... // Allocate
aStruct a;
a.obj = obj;
aStruct b;
b.obj = obj;
int refs = get_references(obj); // refs should now = 2
Any ideas? Workarounds and alternative methods welcome.
No, there’s no implicit way. But you could implement a
reffunction that automatically increases a counter, and anunreffunction to decrement it.And that counter can be something external to any of the
structs. For example, you could use a hash table to keep track of pointer – counter relationships.EDIT
You can also look into
gobjectwhich provides this viag_object_ref/g_object_unref.