fix memory leak closes #3131
Merge commit 'bacc33d6fa79359141707eda3196b1ae02d2cc26'