fix memory leaks and garbage return value from static analyzer

The static analyzer included in Xcode 5.1 Beta 4 caught a few memory leaks, bad return values and type mismatch which are addressed here.