Killing child processes when debugging ends - visual-studio

I have a C++ project in Visual Studio 2008 that, when executed, spawns another process (due to a GUI library that is being used). However when I exit the main process (either using Ctrl-C or by stopping the debugger), the spawned process (and hence the GUI) remains. This is a side-effect of the library being used, and the nature of the project - it is designed to be embedded, and hence never really "exit", but debugging and testing is Windows-based. The orphan process then stops me being able to run the software again, without first killing it.
Using Process Explorer I can see that the spawned process is a child of the process being debugged. I would like a way to, when I end debugging of a project, kill all child processes.
The Chromium project has a macro which will grab child processes in order to attach to them. This is designed to be associated with a button in the VS GUI, and seems to be chrome-specific (that is, you would need a different button for a different project/parent-process).
I have looked into using the DebuggerProcessEvents::OnProcessStateChanged macro, the idea being that when the debugger stops I could walk through the child processes using a similar idea to the Chromium macro and <evil voice>kill them</evil voice>... but this event never seems to be triggered.
Any thoughts? The project is Makefile-based, due to the code being generated outside of Visual Studio, so I will be limited in the options I can set project-wise.

Related

How to execute code when stopping application from debugger

When running an application, the application start many secondary processes that run on their own. When the app close those process are stopped.
My problem is that the only place i could figure out that work is using the Application.ApplicationExit event. It works when running the app and even when it crashes but the problem is when debugging with Visual Studio if you stop the code it doesn't trigger that code and we have couple hundreds of processes to kill.
I am looking for a better way than writing a batch file and list all processes to kill that i have to run every time a dev stop the code from running from within visual studio.
Maybe there is another event that is triggered when an app that is stopped by visual studio that i missed ?
If you want your child processes to always be killed when the parent is terminated, you could consider using a Job Object to associate them all. As each child process is started, add it to the job object shared by the parent; when the parent is terminated, Windows will also terminate all of the processes associated with that job object.

How to break a process on start

With visual studio you can attach to a running process, hit 'pause' (or called break), and even without symbol files or source, you've paused the process and can see a disassembled view.
I would like to achieve this but at the very start of the process. Attaching and pausing as quickly as possible is not the solution I'm looking for :)
For example, if the application was a console based c++ app, gdb can set a break point on main() [or any named function it can find for that matter]. Can something similar be done with visual studio?
But this question is for the more general case - I'd like to be able to start a process and have it pause immediately upon entry (immediately after the kernel launches the process).
http://blogs.msdn.com/b/greggm/archive/2008/09/12/attaching-a-debugger-at-startup.aspx
There are some level of support. But if I were you, I would use WinDbg directly.

Visual studio process Unable to copy file

i have a visual studio 2010 , when i run project normal runs, and when i close window then make some changes and run, they says
"Error 5 Unable to copy file "obj\x86\Debug\Passport.exe" to "bin\Debug\Passport.exe". The process cannot access the file 'bin\Debug\Passport.exe' because it is being used by another process. Passport"
as i see in Passport.exe still in task manager. why?
(when i end that process again i can build project)
NOTE for my other projects no that problem, only for this.
Many thanks who can help :)
There are many reasons why this could happen, but basically it boils down to your passport process or a child process of it, not closing as soon as you "close the window".
Things to check:
1. Extra threads not being terminated (worker threads not complete etc.)
2. Debug, breakpoints causing closure process to hang.
First I would remove all breakpoints and then look at what processes you may be running that could be holding up process closure e.g. worker threads, file writing, hardware access.
Sometimes it's best to kill processes using the stop button in Visual Studio.

What's the advantage for 'attach to process' compared with 'Start Debugging'?

I am new to programming.
I know only Start debug before. Maybe start debug suit for some small application develop better.
I found Visual studio IDE provide another method of attach to process for using.
When & Why must I use the attach debugging?
Such as multi-threading application debugging. Client/Service application debugging. etc. Thank you.
Sometimes you need to debug a process started by another program.
For example you need a reliable solution and in order to protect against access violations, memory leaks and other barely recoverable stuff, you have a master program and several worker programs. The master program starts the worker program and passes parameters to it. How do you debug a worker program which is not intended to be started by anything except the master program?
You use "attach to process for that".
Typically you do it this way: insert a statement that blocks the worker program for some time - for example, call Sleep() for 15 seconds. Then you kindly ask the master program to start the worker program. When the worker program is started it blocks and you now have 15 seconds to attach to it.
This way you can debug almost any issues - problems at early startup stages, wrong parameters, etc, which you wouldn't reliably reproduce with "run with debugging".
Attaching to a process is useful if you don't want to debug right from starting the process. For example, debugging usually slows down execution, so it can be quicker to start the app, get it to a state where a bug appears, and then attach a debugger.
It's also useful if you already have an external means of launching the process that you don't want or can't to import into the IDE.
Start debugging from VS launches an instance of the VS webserver and attaches the debugger to it.
Attach to process allows you to attach to any process and debug it, usually you'd do this to your instance of w3wp.exe running your code in IIS
Attach to process is mostly used when you can't run the application from Visual Studio.
For example, if it's a service or if it is a process that has run for a long time and now you want to start debugging it.
Sometimes you also want to debug a remote process, not on your machine - and you can do that using attach to process.

Automatically attach vs2005 debugger to a child processes

I have a main C++ app built in Visual Studio 2005, called A.exe. It spawns a child process, B.exe. I run process A in the debugger by hitting F5 -- the only way I know to hit breakpoints in process B is to wait for A to kick it off, then run Debug -> Attach to Process, and manually select B.exe. This doesn't work very well if I need to debug initialization code in process B -- I have to start putting in 10 second sleeps at the beginning.
Is there some trick in the vs2005 GUI that I'm missing?
I'm using native code, by the way.
Thanks,
Nathan
You can tell Windows to automatically attach the debugger when a certain process is started (by specifying the process name in a registry setting).
The details are here:
http://msdn.microsoft.com/en-us/library/a329t4ed(v=vs.100).aspx
You'd be hard pushed to make use of the debugbreak command in the child process as the debug process is not yet attached.
However, there is another that may be of use. Seeing as your creating the process, you'll have the handle to it. So give the DebugBreakProcess function a whirl.

Resources