pointer validity check to fix a crash