My ex and I never seem to work out

My ex and I have been dealing with each other for almost 3 years now & every time we try to work things out it always fails. I know deep down a relationship will never work with him but we always find our way back into each other lives. I been hurt so bad by this man I don't know why I still love him after what he's done and put me though but I do and see potential in him, he's just mentally fucked up. He's not a bad guy. I know I deserve better but I'm so attached to him & when I try to let go, I always seem to allow him back in :(  He told me he still loves me and wants to be with me but my attitude is so bad and the way I act. Idk what to do