Added NULL safety checks

These were reported as potential flaws by VeraCode scan.
Fixed potential memory leak
Merge pull request #100 from lixar/veracode-fixes

Added NULL safety checks