Clear ai on return, also in case of error

Coverity CID#1238644