[SOLVED] Question about (broken) handling refcounter

I’ve discovered several cases of broken ref counter (in particular, just ungrouping nodes or creating new material).

It seems like refcounters should be handled in BKE_id_copy_ex by this code:

  /* Update ID refcount, remap pointers to self in new ID. */
  struct IDCopyLibManagementData data = {
      .id_src = id,
      .id_dst = *r_newid,
      .flag = flag,
  BKE_library_foreach_ID_link(bmain, *r_newid, id_copy_libmanagement_cb, &data, IDWALK_NOP);

But to make it work, the id_copy_libmanagement_cb should be called with IDWALK_CB_USER set in cb_flag:

/* Increase used IDs refcount if needed and required. */
  if ((data->flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0 && (cb_flag & IDWALK_CB_USER)) {

And it’s is not the case in BKE_id_copy. And it’s never the case.

Do I miss something?

Hi, for the records, @mont29 is the developer more familiar with this area.

My suggestion here would be to first report as a bug, so it is easier for another developer to confirm and have a reliable .blend and steps to reproduce.

Once confirmed that this is not working as expected, Bastien (or someone else) can review your patches to fix that.

I’ve recently commited a set of autotests that reveal a bunch of bugs of this matter.

Now I’m exploring the code in hope that I can fix it myself.

This particular line might be a bug that’s actually compensated by another bug somewhere else.

Well, what I missed is that BKE_library_foreach_ID_link do not use the flag directly, but uses a PILE of scary macros to actually call the callback.
The macros call it with IDWALK_CB_USER.

But somehow not for material copypaste or new/clone.