Self-Loathing from Giving into Urge

The other day I had a very stressful day, and the urge came up to grab a pint of ice cream. I reminded myself that I was going to honor my commitment to myself to lose weight (I have a goal to lose 40 lbs. this year), and it wasn’t worth it to sacrifice this goal for immediate gratification. I allowed the urge to be there, and didn’t eat the ice cream. I felt so freaking empowered! It felt amazing.
Fast forward to 2 hours later….the urge to eat the ice cream came up again, and I gave in and ate it. I did stop myself before I ate the whole pint, but I felt so much shame and self-loathing. Thoughts like, “What’s wrong with you?”, “You’re so weak”, “You are a failure”, kept coming up, and I felt awful the rest of the night.
How can I, going forward, stop the self-loathing and shame- especially in those times that I do give in to urges or eat off protocol? It’s NOT useful, and many times sends me on a negative spiral downward into a very dark place (indulging in terrible thoughts about myself). I want to break this self-loathing cycle, but in the moment it feels so real to me, it’s hard to imagine anything else.