fix memory leak closes #3131