Set Next Statement in Delphi?

Posted by on in Blogs
I am a huge fan of Matt Pietrek. I also just found his home page. And guess what, he even worked for Borland!!! Oh and you should read his material - Under the Hood on MSDN, the Windows Internals, System Programming Secrets and Undocumented Windows books, and this (the last one isnt his, but hey it deserves a mention).

Okay, so in his Blog, Matt mentions a "Set Next Statement" - something in VS that allows you to actually move the next instruction executed to a new position. This isn't exclusive to VS, you can do it in Delphi too, it's just not documented well enough. Here's how you do this:

Let's say you have this kick-ass piece of code:

procedure TForm1.Button1Click(Sender: TObject);


procedure TForm1.DoSomethingStupid;
while true do ;
You should never be able to debug this down to the "ShowMessage('Done');" statement, because of the wonderful "while true do ; " code. Now I've faced this problem many-a-times; I just want to get out the DoSomethingStupid function, and move on to the next piece of code which I've got to test. (Recompiling is easy in Delphi, you say. Ha. Not if relaunching of the app involves a login, navigation, entering some specific data and then debugging.)

Set a breakpoint in the "while true do;" line. When the program breaks there, hit Ctrl+Alt+C (or View|Debug Windows|CPU). The windows looks like this:

Right click on the next line (with the ret instruction) and select New EIP.

EIP is the (Extended) Instruction Pointer, which is what makes you a pointy haired boss (PHB). No, that's something else. Anyways.

What happens next is that your ShowMessage call runs! so you're out of the infinite loop, without having to recompile. This is very helpful when you've made an obvious blunder (like in saving a file, you've given an invalid path) but that blunder doesn't affect anything else like parameters, stack etc. (And don't try doing this to get out of a try/except/finally statement - won't work most of the time)

The CPU window is your friend. There are tons more tricks you can use - Danny had an amazing session ("Reading Tea Leaves") in a BorCon long back that led me on to this, but there's not much else I've remembered...


Check out more tips and tricks in this development video: