Memory safety checks to prevent crashing