I am in my mid-40s and am currently mending a broken heart over another failed relationship

I keep getting into relationships with men that are in bad situations. I take them on and help them and do all I can and the relationship still fails. How do I learn to not get in relationships with men in bad situations? I see someone in need of help, I jump in. And it is getting me nowhere. I am alone sad and frustrated.