Various fixes

* Remove useless type casting
* Destroy thread attributes object
* Free unused memory allocations on close