Fixed memory leaks when database queries fail.