fix possible resource leak and make sure there's something to qsort()