core: safety check for double free in q_malloc as in f_malloc

(cherry picked from commit 943a5941f5ab479803fb30e19e9fa607f0f0fe14)