Log messages before freeing data

Seems I introduced a bad memory bug myself. I was logging errors using the freed pointers. The following commit fixes it. 

Regards,