Code review comment for lp://staging/~smspillaz/compiz-libcompizconfig/ccs-object

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

These macros can lead to surprising results in user code:

86 +#define ccsObjectRef(o) \
87 + ((o)->object).refcnt++;
88 +
89 +#define ccsObjectUnref(o, freeFunc) \
90 + ((o)->object).refcnt--; \
91 + if (!((o)->object).refcnt) \
92 + freeFunc (o)

Example of problem:

  if (false) ccsObjectUnref(o, f);

will call f(o).

Rewrite as:
86 +#define ccsObjectRef(o) \
87 + do { ((o)->object).refcnt++ } while(false)
88 +
89 +#define ccsObjectUnref(o, freeFunc) \
90 + do { ((o)->object).refcnt--; \
91 + if (!((o)->object).refcnt) \
92 + freeFunc (o) } while(false)

PS It feels awkward to require freeFunc as a parameter, but I don't have a cleaner alternative suggestion just now.

review: Needs Fixing

« Back to merge proposal