daemon: Fix possible leak and unclear/undefined free behavior

Some versions of gcc complain about this code, as it's using the
cleanup attribute in a loop. Make the code clearer and defined.