Fix cppcheck warning:

'operator=' should check for assignment to self to ensure that each block
of dynamically allocated memory is owned and managed by only one instance
of the class.
Fix few memory leaks found by cppcheck.
Fix few resource leaks found by cppcheck.
Prevent possible double free.