'release' versus '= nil'

Hello! Here are two small programs that assign nil to a class. The first uses release:

// relex releases memory with .release
class cl {
  var ii: int;
}

var v1: cl?;
v1 = new owned cl?(1);
writeln(v1);
v1.release();
writeln(v1);

and the second is brute-force:

// nilex: releases memory??? with '= nil' assignment
class cl {
  var ii: int;
}

var v1: cl?;
v1 = new owned cl?(1);
writeln(v1);
v1 = nil;
writeln(v1);

Both produce the same output, but: My question is: relex.chpl probably returns the memory pointed to by v1 to the operating system orderly (with something like Cā€™s free(ā€¦)): does nilex also free memory orderly, or does it cause a memory leak?

Thanks, Nelson

Hi Nelson,

Assigning a nilable owned nil is well-defined behavior and will not cause a memory leak. Both of your programs will properly release memory for v1.

If you are observing a memory leak in one of your programs then please feel free to file it as a bug on on the Chapel GitHub repository!

Thanks! No, I am not having any real memory leaks with my programs.

1 Like