BUG: Fix memory leak.

Patch is from Julian Taylor. Closes #4652.
Merge pull request #4654 from charris/fix-memory-leak-gh-4652

BUG: Fix memory leak.