U++ framework
Do not panic. Ask here before giving up.

Home » U++ TheIDE » U++ TheIDE: Compiling, Linking, Debugging of your packages » I keep getting the same "cannot open exe for writing" error...
I keep getting the same "cannot open exe for writing" error... [message #9919] Fri, 08 June 2007 16:22 Go to next message
blackbyrus is currently offline  blackbyrus
Messages: 1
Registered: June 2007
Junior Member
Here's an example of the error:
LINK : fatal error LNK1168: cannot open C:\upp\out\MSC71.Debug_full.Gui\Hello.exe for writing

In this specific one, Hello.exe had already compiled 3 times before it suddenly stopped working.

I get it for my (very simple, straight from tutorial) programs, and for the already written example programs that came packaged with the IDE. Sometimes a program will compile a couple of times, I'll make a very minor change, then I will get that error, I change it back, and now it won't compile anymore. I'm confused as to what I may be doing to cause this.
Re: I keep getting the same "cannot open exe for writing" error... [message #9920 is a reply to message #9919] Fri, 08 June 2007 17:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
This usually means that program is still running. Win32 cannot write to the image of running process. Check the process list and kill the process.

Rarely, this can be caused by the problem in the debugger -> in that case, you need to restart TheIDE.
Re: I keep getting the same "cannot open exe for writing" error... [message #50478 is a reply to message #9920] Tue, 06 November 2018 17:06 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Hello all

I get very frequently this error.
The system is Windows 10 and compiler is VS 2017.
It is annoying as only way to avoid it is restarting TheIDE or changing exe name.
Thank you.


Best regards
Iñaki
Re: I keep getting the same "cannot open exe for writing" error... [message #50482 is a reply to message #50478] Tue, 06 November 2018 19:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 06 November 2018 17:06
Hello all

I get very frequently this error.
The system is Windows 10 and compiler is VS 2017.
It is annoying as only way to avoid it is restarting TheIDE or changing exe name.
Thank you.


Are you using debugger?
Re: I keep getting the same "cannot open exe for writing" error... [message #50483 is a reply to message #50482] Wed, 07 November 2018 12:44 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Yes.

Best regards
Iñaki
Re: I keep getting the same "cannot open exe for writing" error... [message #50484 is a reply to message #50483] Wed, 07 November 2018 16:44 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Running Sysinternals Process Explorer, it appears that the program is already running.
However in TheIDE there is no option available for stopping any process, so TheIDE thinks that the process is already stopped.
This problem happens sometimes, every 5-10 compilings of the same source.


Best regards
Iñaki
Re: I keep getting the same "cannot open exe for writing" error... [message #50600 is a reply to message #50484] Sun, 25 November 2018 17:40 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
For me it happens after previous debugging.
(I am on Windows10 64 Bit)
It happens for me when I am using MSVC17 or MSVC17 64 Bit in TheIDE and only after debugging.

In the Windows Taskmanager it can be seen, that the previously debugged process is still attached to TheIDE.
If I choose "Debug" in the Windows Taskmanager then Windows tries to attach the VSstudio debugger.

Attaching VSStudio debugger fails.

There is an error message, which says "There is another debugger attached to this process, which is not configured to handle this kind of exception".

It seems to me, it happens, when the debugee was stopped and had some kind of exception during shutdown and this probably prevented the debugger from detaching.

These are just my observations. (And educated guesses) Hope this is useful.
I have no experience with windows programming, but have used debuggers on DOS and Linux some decades ago.

[Updated on: Sun, 25 November 2018 20:24]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50610 is a reply to message #50600] Mon, 26 November 2018 10:54 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
peterh wrote on Sun, 25 November 2018 17:40
For me it happens after previous debugging.
(I am on Windows10 64 Bit)
It happens for me when I am using MSVC17 or MSVC17 64 Bit in TheIDE and only after debugging.

In the Windows Taskmanager it can be seen, that the previously debugged process is still attached to TheIDE.
If I choose "Debug" in the Windows Taskmanager then Windows tries to attach the VSstudio debugger.

Attaching VSStudio debugger fails.

There is an error message, which says "There is another debugger attached to this process, which is not configured to handle this kind of exception".

It seems to me, it happens, when the debugee was stopped and had some kind of exception during shutdown and this probably prevented the debugger from detaching.

These are just my observations. (And educated guesses) Hope this is useful.
I have no experience with windows programming, but have used debuggers on DOS and Linux some decades ago.


OK, then it is "known issue". We are well aware about this, but have found not solution yet. It looks like Microsoft .dll we are using to get symbolic information from the .exe fails to close same handles to .exe.

For now, if I run into this, I simply restart theide.

If you want to try to resolve this, the critical code is in Pdb::Stop (ide/Debuggers/Pdb.cpp). Problem is that it works "most of time"... Sad
Re: I keep getting the same "cannot open exe for writing" error... [message #50613 is a reply to message #50610] Mon, 26 November 2018 12:45 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Yes, when I see the problem then I try to solve it, or at least look for a way to reproduce it.

I see this problem frequently, because I use the debugger frequently.
Not that I write software, but I work through the examples and if I dont understand something, then I use the debugger to see how stuff works.

I could try the tools from sysinternals to investigate this problem furter, when it happens.

My observation is, sometimes it doesnt happen and sometimes it happens frequently, with the debugee being substancially unchanged.

It is not reproducible, this could be a hint for uninitialized variables, dangling pointer or race condition.

If the problem is in the microsoft dll then only a quick'n dirty workaround would be possible:

Before the build starts, delete the target *.exe file.
Possibly the Windows API has a way to get the filehandle and close and delete the file? (I dont know this; this is just an unproven idea)
If this is not possible rename it and delete it at next start.
Possibly move the exe to temp then windows would delete it.

[Updated on: Mon, 26 November 2018 13:30]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50617 is a reply to message #50613] Mon, 26 November 2018 17:10 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
		if(hProcess != INVALID_HANDLE_VALUE) {
			DebugActiveProcessStop(processid);
			TerminateProcess(hProcess, 0);
			
                        // Wait for max. 10 seconds. This should solve 99.9% of all problems. I hope so ;-)
                        // TODO: If a complex unknown and possibly buggy process is terminated violently, 
                        // then the error codes are hard to interpret and should be displayed to the user.
                        WaitForSingleObject(hProcess,10000);
                        

                        while(threads.GetCount())
				RemoveThread(threads.GetKey(0)); // To CloseHandle
			UnloadModuleSymbols();
			SymCleanup(hProcess);
			CloseHandle(hProcess);
		}


Possibly I have found a problem.
The TerminateProcess() systemcall is asynchronous and returns immediately, eventually before the process is actually terminated.
Also it kills all threads.
(If the documentation is correct)

Possibly a WaitforSingleObject() call must be inserted here.

https://docs.microsoft.com/en-us/windows/desktop/api/process threadsapi/nf-processthreadsapi-terminateprocess

I think I will try this, when I am at home.

In a later stage the waiting time could be measured, and if it is longer than 9 seconds then the error codes could be displayed to the user and the user should decide if he/she wants to wait longer and retry or ignore the error codes.
This is more work to implement and not necessary for now.
If this approach works, I would be happy to try to write the code. Wink

Edit:

I have now compiled this and it works so far.
If the aforementioned problem shows up again can only be said after some hours of trial.

[Updated on: Mon, 26 November 2018 22:58]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50618 is a reply to message #50617] Tue, 27 November 2018 00:14 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Hello Peterh

Good find.

Quote:
The TerminateProcess() systemcall is asynchronous and returns immediately, eventually before the process is actually terminated.
Also it kills all threads.
(If the documentation is correct)
TerminateProcess() does not kill child processes.

To handle that Bazaar/Functions4U includes TerminateChildProcesses(). It kills all processes tree.

It may be dangerous, as it does not follow the proper killing order but, as using TerminateProcess() is a brute force process end, TerminateChildProcesses() just tries to do the same thing, but in depth.

Edit: Similar focus is used in PauseChildThreads() with surprising effectivity even for child GUI software.


Best regards
Iñaki

[Updated on: Tue, 27 November 2018 00:19]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50619 is a reply to message #50618] Tue, 27 November 2018 08:54 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Then TerminateChildProcesses() might be a better solution if it doesnt block.

In any case terminating an unknown eventually buggy process which can have childprocesses, threads and pending IO can go wrong.

Cleanly terminating is without problems in simple cases and might be impossible in complicated cases.
Compromises are necessary. Terminating is always an emergency brake.

I must admit, I am unable to write this debugger and I value and respect your work.
I have only focussed to this problem and went through the code step by step and had an idea what to look for: uninitialized variables, dangling pointers, buffer overflows, race conditions.
And in this case it seems to be a race condition.

BTW, if there are childprocesses, then there is the question if it is good to terminate them. Possibly they can be terminated with the taskmanager or Sysinternals Process explorer or a debugger can been attached to them?

All the best

[Updated on: Tue, 27 November 2018 09:11]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50620 is a reply to message #50619] Tue, 27 November 2018 09:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
peterh wrote on Tue, 27 November 2018 08:54

I must admit, I am unable to write this debugger and I value and respect your work.
I have only focussed to this problem and went through the code step by step and had an idea what to look for: uninitialized variables, dangling pointers, buffer overflows, race conditions.
And in this case it seems to be a race condition.


I absolutely agree - I have to admit that I have completly missed the fact that Terminate is nonblocking.

IMO what we need there is active wait, with Progress for cancelation (for the case that Terminate fails). Or maybe just keep trying for only 3s.

Mirek
Re: I keep getting the same "cannot open exe for writing" error... [message #50621 is a reply to message #50620] Tue, 27 November 2018 09:50 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
So I have tried a quick patch to resolve this based on the new info. It is in the trunk.

I would appreciate testing... Smile

Mirek
Re: I keep getting the same "cannot open exe for writing" error... [message #50623 is a reply to message #50621] Tue, 27 November 2018 10:07 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Thank you!
So this is in nightly build tomorrow?

I am going to test and report in the evening, when I am at home.
Dayover I am at Job and look occasionally into the forum.
My Job is not programming, but industrial electronics hardware.

My idea is this:

In normal cases termination should be quicker than some 100 milliseconds.
Only if there are problems like pending IO it would last longer.
I do not know if in this cases Windows waits or if it terminates the process in the hard way, leaving open serial ports, USB devices and Files behind. Wink

However the timeout in WaitForSingleObject() must have a meaning and nobody knows how the next version of Windows or other versions will handle this.
So, if a timeout happens there must be an unusual problem and only the user/programmer can know for the reason.

It is then questionable wether the termination would succeed and the user should be asked, when a timeout happens if he wants to wait, instead of closing the process-handle, possibly unsuccessful.
Inbetween he/she could use other system-tools to debug or ignore the problem and restart the IDE and possibly chooses to reboot afterwards.

We use hardware testprograms here.
Some of the devices we test are defective and have unforseeable problems. This is, why we test. Wink
We connect potentially defective devices to the USB Port and disconnect them, semiautomatically some hundreds a day.
I have often seen USB devices vanish after a crash and in this case there is no other choice than to reboot after such an event.
So if such a situation is encountered while debugging the test program, the debugger must leave the decision about closing a process handle after timeout to the debuggers user.

[Updated on: Tue, 27 November 2018 13:17]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50625 is a reply to message #50623] Tue, 27 November 2018 13:25 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Quote:
So I have tried a quick patch to resolve this based on the new info. It is in the trunk.

This morning I have used your patch in a tough way, stopping the debug with SHIFT+F5.
For now it goes right Smile


Best regards
Iñaki
Re: I keep getting the same "cannot open exe for writing" error... [message #50629 is a reply to message #50625] Tue, 27 November 2018 21:09 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
I have now downloaded Pdb.cpp from the Git website and compiled this into my IDE.
I tortured it somehow, a lot of breakpoints, Shift F5, singlestep inside a thread
and always rebuilt the debugee (GUIlock).

No problems so far.
I have also seen with this nightly build Alt J is working again and Mathtools compiles again.

[Updated on: Tue, 27 November 2018 21:14]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50636 is a reply to message #50629] Wed, 28 November 2018 10:49 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Hi,

I did download nightly build 12584 this morning.
The sources included do not contain the updated code for Pdb::Stop()
So it is unclear to me if the binary has the updated code.
Re: I keep getting the same "cannot open exe for writing" error... [message #50637 is a reply to message #50636] Wed, 28 November 2018 10:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
peterh wrote on Wed, 28 November 2018 10:49
Hi,

I did download nightly build 12584 this morning.
The sources included do not contain the updated code for Pdb::Stop()
So it is unclear to me if the binary has the updated code.


It was revision 12585. So perhaps tomorrow...
Re: I keep getting the same "cannot open exe for writing" error... [message #50651 is a reply to message #50637] Thu, 29 November 2018 12:21 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1428
Registered: September 2007
Ultimate Contributor
I tested with 12587.

The issue is not fixed.

In the past I would get a hang up when the process locked and I had to quit TheIDE.

Now, I get a message telling me that the binary can't be opened for writing. So I guess it is an improvement. The process can't be killed by windows manager and I need to shut down TheIDE.

A few months ago I tried everything to fix this but gave up. I was using WinAPI (WaitForSingleObject if I remember correctly) to wait for the process. Yet the process would not finish and TheIDE would hang forever.

			dword exitcode;
			int start = msecs();
			while(GetExitCodeProcess(hProcess, &exitcode) && exitcode == STILL_ACTIVE && msecs(start) < 3000)
				Sleep(1);


This behaves better than WaitForSingleObject since it won't lock forever, but I suppose testing GetExitCodeProcess after 3 seconds will still give an active result in the case where the exe can't be written too. Maybe that should be logged somewhere and shown in TheIDE?

There must be a more aggressive way o killing a process as a fail safe?


Re: I keep getting the same "cannot open exe for writing" error... [message #50652 is a reply to message #50651] Thu, 29 November 2018 12:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
cbpporter wrote on Thu, 29 November 2018 12:21
I tested with 12587.

The issue is not fixed.

In the past I would get a hang up when the process locked and I had to quit TheIDE.

Now, I get a message telling me that the binary can't be opened for writing. So I guess it is an improvement. The process can't be killed by windows manager and I need to shut down TheIDE.

A few months ago I tried everything to fix this but gave up. I was using WinAPI (WaitForSingleObject if I remember correctly) to wait for the process. Yet the process would not finish and TheIDE would hang forever.

			dword exitcode;
			int start = msecs();
			while(GetExitCodeProcess(hProcess, &exitcode) && exitcode == STILL_ACTIVE && msecs(start) < 3000)
				Sleep(1);


This behaves better than WaitForSingleObject since it won't lock forever, but I suppose testing GetExitCodeProcess after 3 seconds will still give an active result in the case where the exe can't be written too. Maybe that should be logged somewhere and shown in TheIDE?

There must be a more aggressive way o killing a process as a fail safe?




Perhaps we could try to add Exclamation after that if process is still active - at least that would show us that this is the problem.
Re: I keep getting the same "cannot open exe for writing" error... [message #50653 is a reply to message #50652] Thu, 29 November 2018 12:27 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1428
Registered: September 2007
Ultimate Contributor
mirek wrote on Thu, 29 November 2018 13:23

Perhaps we could try to add Exclamation after that if process is still active - at least that would show us that this is the problem.


Yes please!

Also, since I updated, when running command line programs with Ctrl-F5, I get this lovely output message:
Quote:
<--- Finished in (-5371:-45.-1), exitcode: 0 --->

Re: I keep getting the same "cannot open exe for writing" error... [message #50655 is a reply to message #50651] Thu, 29 November 2018 13:41 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
The question is, if your process needs more time than 3 seconds to terminate.
The other question is can it been terminated with this method at all?
If it hangs in blocking IO in kernel mode, possibly it cannot be terminated.
I do regulary see applications that do serial IO via the .NET and if these crash, then
Windows does not shut down because it waits for the process forever.

This is why I proposed tho give report to the user.

There could be a message box:

"Process termination timeout"
"Wait and retry?" "Ignore and try to Close anyway"

WaitForSingleObject should return these Values:

WAIT_OBJECT_0 means the thread has stopped.
WAIT_TIMEOUT means the thread is running.

Not that I have practical experience with this. This is what I found in MSDN.

Edit:
Just for explanation:
Even if the processhandle is closed and the process detached the file cannot been deleted or modified as long as the process is running.
This is, because windows needs the .exe file for virtual memory paging, when the process is still running.
In this case the file could be renamed and marked for deletion.

[Updated on: Thu, 29 November 2018 16:22]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50656 is a reply to message #50653] Thu, 29 November 2018 14:10 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
cbpporter wrote on Thu, 29 November 2018 12:27
mirek wrote on Thu, 29 November 2018 13:23

Perhaps we could try to add Exclamation after that if process is still active - at least that would show us that this is the problem.


Yes please!

Also, since I updated, when running command line programs with Ctrl-F5, I get this lovely output message:
Quote:
<--- Finished in (-5371:-45.-1), exitcode: 0 --->



Added & fixed...
Re: I keep getting the same "cannot open exe for writing" error... [message #50659 is a reply to message #50651] Fri, 30 November 2018 09:01 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Hi,

I do not know how Windows terminates a process.
I have however done WIN32 programming years ago before, even Kernel debugging. (Debugging a kernel mode driver)
So I have ideas about it.
Windows might inject an ExitProcess call into the code of the process in order to terminate it with the least damage possible.
This would not work, if the process doesnt run (e.g. hangs in blocking IO in kernel mode).
Also this can be delayed, if heavy background activity happens, e.g. swapping and it could be necessary to swap portions of the debugged process in.
In these cases I can imagine, TerminateProcess would need time or might be impossible.

All the best,

Peter

[Updated on: Fri, 30 November 2018 09:04]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50660 is a reply to message #50659] Fri, 30 November 2018 11:48 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
I have seen this same error with MinGW on Window7 32 Bit.

(): Linking has failed
(): C:/upp/bin/mingw64/32/bin/../lib/gcc/i686-w64-mingw32/7.1.0/ ../../../../i686-w64-mingw32/bin/ld.exe: cannot open output file C:\up
p\out\reference\MINGW.Debug.Debug_Full.Gui.Mt.Noblitz\CoWork .exe: Permission denied
(): collect2.exe: error: ld returned 1 exit status

It happens reproducibly when I start CoWork.exe in debug mode and terminate it with stop.
The process stays in memory after termination.
In this case the terminated process can be removed with the Windows Taskmanager.

Edit:
I retried this with MingW 32 and 64 on a faster Computer, running Windows 10 64 Bit.
There it doesnt happen.

[Updated on: Sat, 01 December 2018 14:13]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50688 is a reply to message #50660] Wed, 05 December 2018 21:52 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Hi,

I want to add this, because it might help others:

I went back to the 2018.1 stable build, because I had problems with the nightly builds.
With 2018.1 the aforementioned problem occurs frequently to me.
Then I added WaitForSingleObject(hProcess,10000) to Pdb::Stop() as aforementioned.
This did not cure the problem, but did also not cause any additional effects like delays.

Then I did see, the Ide project had the flags "GUI" and no other.
So I recompiled Theide with flags "GUI MT", because I believe, it could be multithreaded.
Apparently this cured the problem for me. It did not happen again since 2 hours now.

All the best,

Peter

[Updated on: Wed, 05 December 2018 21:57]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50690 is a reply to message #50688] Wed, 05 December 2018 23:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
peterh wrote on Wed, 05 December 2018 21:52

Then I did see, the Ide project had the flags "GUI" and no other.
So I recompiled Theide with flags "GUI MT", because I believe, it could be multithreaded.
Apparently this cured the problem for me. It did not happen again since 2 hours now.


No, that just shows how elusive this issue is. "MT" flag is ignored since ~2016. We are now MT always.

Mirek
Re: I keep getting the same "cannot open exe for writing" error... [message #50692 is a reply to message #50690] Wed, 05 December 2018 23:40 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Could be.
Otherwise the difference was drastic.
I had written "MT" into the compiler options.
Microsoft docs for VC 2015 say /MT will define the flag _MT and this will tell the linker to link the multithreaded libraries and/or DLLs
(I use VC2017)
Is this info outdated?

[Updated on: Wed, 05 December 2018 23:45]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50694 is a reply to message #50692] Thu, 06 December 2018 10:32 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
peterh wrote on Wed, 05 December 2018 23:40
Could be.
Otherwise the difference was drastic.
I had written "MT" into the compiler options.
Microsoft docs for VC 2015 say /MT will define the flag _MT and this will tell the linker to link the multithreaded libraries and/or DLLs
(I use VC2017)
Is this info outdated?


Builders/MscBuilder.cpp 275

-MT is hardwired. It is always there...

Mirek
Re: I keep getting the same "cannot open exe for writing" error... [message #50695 is a reply to message #50694] Thu, 06 December 2018 10:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
Perhaps we should now concentrate on

			while(threads.GetCount())
				RemoveThread(threads.GetKey(0)); // To CloseHandle
			UnloadModuleSymbols();
			SymCleanup(hProcess);
			CloseHandle(hProcess);


part... I guess with current patch, we can be fairly sure that process is terminated for good (otherwise it would display an Exclamation)
Re: I keep getting the same "cannot open exe for writing" error... [message #50697 is a reply to message #50695] Thu, 06 December 2018 19:23 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Thank you very much.
I checked it, and it is as you said.
As this is an annoying problem for me, I think I will insert some debug messages into my own TheIde compilation and try,
if I can find something out that is helpful.

All the best,

Peter
Re: I keep getting the same "cannot open exe for writing" error... [message #50700 is a reply to message #50697] Thu, 06 December 2018 20:53 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
I have now changed the code in the following way:

		if(hProcess != INVALID_HANDLE_VALUE) {
			DebugActiveProcessStop(processid);
			TerminateProcess(hProcess, 0);
			DWORD retval = MsgWaitForMultipleObjects(
                                      1, 		//DWORD        nCount,
                                      &hProcess, 	//const HANDLE *pHandles,
                                      true, 		//BOOL         fWaitAll,
                                      10000, 		//DWORD        dwMilliseconds,
                                      0  		//DWORD        dwWakeMask
				      );
			if (retval == WAIT_TIMEOUT)   Exclamation("WAIT_TIMEOUT");
			if (retval == WAIT_ABANDONED) Exclamation("WAIT_ABANDONED");
		
			WaitForSingleObject(hProcess,10000);
			while(threads.GetCount())
				RemoveThread(threads.GetKey(0)); // To CloseHandle
			UnloadModuleSymbols();
			SymCleanup(hProcess);
			CloseHandle(hProcess);
		}



I did this because I have read, the WaitForSingleObject() call can return prematurely under some special circumstances.
I cannot explain it, because Microsoft cannot explain it clearly. Twisted Evil
The explanations given are misunderstandable or ambigous and there are subtile typos sometimes. And no examples Crying or Very Sad
I simply try it, if someone understands this perfectly, it will be appreciated.
After I did this, something changed: Stopping the debugee needs about 4 seconds, which is perfectly ok for me.
Edit: I added some debug code
The reason for the 4 seconds delay is not WAIT_TIMEOUT and is not WAIT_ABANDONED.
I will add some code to log the reason tomorrow. (Still I am learning Upp, needs more time than usual)
Ok, I will try this over the next few days and then report again.

So long,

Peter

[Updated on: Thu, 06 December 2018 22:40]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50712 is a reply to message #50700] Sat, 08 December 2018 17:00 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Exclamation() crashes TheIde, when called inside Pdb::Stop().
Possibly this is a non GUI thread and it must be locked, but I dont know.

So I have built in extensive logging, I log all return values.

I cannot tell more news, because it did yet not happen again with the aforementioned code modification.
Re: I keep getting the same "cannot open exe for writing" error... [message #50713 is a reply to message #50712] Sat, 08 December 2018 18:33 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Now it happened again.

DebugActiveProcessStop returned false, this means it failed.
Probably the debugger could not detach.
In turn, TerminateProcess failed.

"terminated flag" = terminated flag
terminated = false //This is OK
"DebugActiveProcessStop" = DebugActiveProcessStop
retval = 0x0 //Boolean, Failed
"TerminateProcess" = TerminateProcess
retval = 0x0 //Boolean, Failed
"WaitForSingleObject" = WaitForSingleObject
retval = 0x102 //WAIT_TIMEOUT
"CloseHandle" = CloseHandle
retval = 0x1 //Succeeded

[Updated on: Sat, 08 December 2018 18:47]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50717 is a reply to message #50712] Sat, 08 December 2018 19:27 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
New Code:
void Pdb::Stop()
{
	DWORD retval;
	LOG("---------------------------------------");
	LOG("terminated flag");
	DUMP(terminated);
	if(!terminated) {
		terminated = true;
		SaveTree();
		String fn = ConfigFile("TreeTypes.txt");
		FileOut out(fn);
		for(int i = 0; i < treetype.GetCount(); i++)
			out << treetype.GetKey(i) << "\r\n" << treetype[i] << "\r\n";
		StringStream ss;
		Store(callback(this, &Pdb::SerializeSession), ss);
		WorkspaceConfigData("pdb-debugger") = ss;
		if(hProcess == INVALID_HANDLE_VALUE)
          { LOG("hProcess Handle is invalid handle value");};

		if(hProcess != INVALID_HANDLE_VALUE) {
			LOG("DebugActiveProcessStop");
			for(int i=0; i<10; i++){
				if (retval = DebugActiveProcessStop(processid)) break;
				DUMPHEX(retval);
            	                Sleep(100);
			}
			DUMPHEX(retval);
			
			retval = TerminateProcess(hProcess, 0);
			LOG("TerminateProcess");
			DUMPHEX(retval);
			retval = WaitForSingleObject(hProcess,10000);
			LOG("WaitForSingleObject");
			DUMPHEX(retval);
#ifdef NEVEREVER
			retval = MsgWaitForMultipleObjects(
							1, 			//DWORD        nCount,
							&hProcess, 	//const HANDLE *pHandles,
							true, 		//BOOL         fWaitAll,
							10000, 		//DWORD        dwMilliseconds,
							0  			//DWORD        dwWakeMask
							);
			LOG("WaitForMultibleObjects");
			DUMPHEX(retval);
#endif
			while(threads.GetCount())
				RemoveThread(threads.GetKey(0)); // To CloseHandle
			UnloadModuleSymbols();
			SymCleanup(hProcess);
			retval = CloseHandle(hProcess);
			LOG("CloseHandle");
			DUMPHEX(retval);
		}
		StoreToGlobal(*this, CONFIGNAME);
		IdeRemoveBottom(*this);
		IdeRemoveRight(disas);
	}
}

[Updated on: Sat, 08 December 2018 19:29]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50718 is a reply to message #50717] Sat, 08 December 2018 19:50 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Apparently it happened again.
This time however, with the new code and it worked as intended.
But not completely, because TerminateProcess failed.
Possibly the process was already terminated, because WaitForSingleObject
succeeded with retval == WAIT_OBJECT_0.

Any way, TheIde had no problem after this.

---------------------------------------
terminated flag
terminated = false
DebugActiveProcessStop
retval = 0x0
retval = 0x1
TerminateProcess
retval = 0x0
WaitForSingleObject
retval = 0x0
CloseHandle
retval = 0x1

[Updated on: Sat, 08 December 2018 19:58]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50720 is a reply to message #50718] Sun, 09 December 2018 10:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14290
Registered: November 2005
Ultimate Member
So if I understand correctly, the key ingredient is to retry the DebugActiveProcessStop ?
Re: I keep getting the same "cannot open exe for writing" error... [message #50722 is a reply to message #50720] Sun, 09 December 2018 11:07 Go to previous messageGo to next message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Yes, this where my findings.

The solution is, if it fails then wait 100 ms.
So the debugee can run and hopefully comes to a state where it succeeds.
In all normal cases this should work at the 2nd try.

The long timeouts are only for non-normal problem cases which we hopefully never will have. Rolling Eyes

Edit:
I did not expect this. I simply logged the results of all system calls and then did see it.

Then however, it was clear to me:
In all cases previously known to me the process could not be removed with the taskmanager.
The reason is, because the debugger is still attached.
So this must be the general reason.

[Updated on: Sun, 09 December 2018 13:54]

Report message to a moderator

Re: I keep getting the same "cannot open exe for writing" error... [message #50821 is a reply to message #50651] Fri, 04 January 2019 09:42 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3458
Registered: August 2008
Senior Veteran
Yes, it follows failing Sad

Best regards
Iñaki
Previous Topic: error: expected unqualified-id before '=' token
Next Topic: clang or MinGW
Goto Forum:
  


Current Time: Mon Apr 27 19:24:17 GMT+2 2026

Total time taken to generate the page: 0.01369 seconds