Graceful exit

It's necessary to do serious checks for the memory leaks using
tools like valgrind. This patch tries to implement a graceful
exit for all tvheadend components and free allocated memory.

Also, some memory leaks were fixed.