cap: fix issues with waserror and memory leaks