fix memory problems pointed out by valgrind in AE. See the PR comments for more details.
Merge pull request #5389 from jimfcarroll/ae-mem-fixes

fix memory problems pointed out by valgrind in AE.