fixing double free

* missing pointer reinitialization to NULL after a free() caused a double free.
* adding test case associated with this bug