New Issue: Memory management issues with 'set.add'

17477, "e-kayrakli", "Memory management issues with 'set.add'", "2021-03-26T23:21:46Z"

set.add's current implementation is prone to memory corruption and possibly leaks. It is not clear to me how to workaround that. Simplified implementation looks like:

    proc _addElem(pragma "no auto destroy" in elem: eltType) {
      use Memory.Initialization;


      on this {
        ... use low-level move to put `elem` in the set
      }

    }

The argument with in intent has no auto destroy because we want to move that copy to the set directly to avoid extra copies. But we do create an extra copy because of RVF and move that copy into the set, instead. But because that copy is created via RVF, it is auto destroy'ed, and set refers to junk memory at the end of the on block. This is an issue if the element type is a serializable record. A solution for that might be to make RVF respect the flags on the outer variable.

I think this could also cause leaks, because the formal as no auto destroy and we don't actually put that formal into the set. So, I suspect we leak that copy.

Here's a reproducer:

use Set;

var s = new set(string);

on Locales[1] {
  var myStr = "myStr";
  s.add(myStr);
}

writeln(s);

this crashes with ASAN.