Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to attach a debugger to a process in VC++ at just the right point in time?

When debugging, sometimes you need to attach an already running process instead of just starting the application in a debugger.

It's common for myself to put in a Sleep() or MessageBox call, so that it's easier to attach a debugger. I worry that some of these may be committed eventually to source control.

What is the best thing to do to avoid this situation while still delaying enough time so that you can attach your debugger to a running process?

Guarding the Sleep or message box with an #ifdef _DEBUG is one way, but I'm wondering if there is a better way.

With a Sleep you also have the problem that you may not attach in time. With a MessageBox you have the problem that you may be remotely debugging, or debugging a process that has no visible GUI (example running as a service on Vista)

like image 611
Brian R. Bondy Avatar asked Mar 19 '09 18:03

Brian R. Bondy


1 Answers

another variant, which I sometimes use is

while( !::IsDebuggerPresent() )
    ::Sleep( 100 ); // to avoid 100% CPU load

it should just silently wait until you attach your debugger to the process.

like image 184
Roman Kruglov Avatar answered Oct 20 '22 19:10

Roman Kruglov