Fix analyzer warnings

These changes fix a bunch of warnings and memory leaks (double retains, missing releases)