Static analysis [WIP] [Help needed]

I have been using cppcheck 1.60 and trying to go through and fix it's complaints. The amount of complaints is much higher than I can hope to resolve, any help would be appreciated.

Given the huge amount of things that are changed so far this should not be merged without a decent amount of testing.