Multiple resource leak and NULL pointer dereferencing fixes