Fix heap corruption
Merge pull request #1027 from igorko/master

Fix heap corruption