Fixed various resource leaks.
Fixed various dereferenced null pointers.