mem: fix potential resource leak on freeing