fix memleak, based upon a patch by Wolfram Gloger