Protect nullptr dereference and fix counters (follow !3188) See merge request !3256 (cherry picked from commit ffc71be3)