fix order of free operations and handle ns='' in dns context allocation