added apply_recover functional to autodiff to catch, recover memory, rethrow, fixing leak in #543