uses free(mem) to avoid memLeak cppcheck error (when using mem = 0)