I have written an app in C++ from Visual Studio 2008, running on Windows 7, that runs fine, using either the debug or release versions, when under the control of the debugger, but when running standalone, using either the debug or release versions, it also runs fine except whenever I click on any unrelated window, say a file explorer window, whereupon the app hangs without any warnings from Windows, I see the little circle thing.
The code is doing something rather computationally intensive, accessing data from a 10Mb global array, and it is well within the 2Gb limit of 32 bit Windows. I have checked for the obvious things, such as uninitialized variables, infinite loops, and the like, I am not allocating any big local arrays, but have found nothing wrong. The code is running directly from the UI thread, blocking, but I do not care as there is nothing else to do till that task completes. Alternatively, I could put this code in its own worker thread communicating back to the UI thread by an interlocked buffer, but this seems redundant. I've tried this on two different machines running Windows 7 and get identical behavior. Is there something about Windows process management that I am overlooking? Is there a way to tell whether there is some sort of memory corruption going on that could cause some other process to affect the app's process?
[Edit1 by spektre] just copied user3481340's code from comment to be readable
I do not think that the computational time, which is about an hour
has anything to do with the problem.
Rather, the windows messaging for the edit box is getting messed-up.
The relevant code is:
int textlen=GetWindowTextLength(Editwin);
int k=strcspn(messagebuf,"\n");
if(k<strlen(messagebuf))textlength=strlen(messagebuf)-k;
else textlength+=k;
SendMessage (Editwin, EM_SETSEL, (WPARAM)textlen, (LPARAM)textlen);
SendMessage (Editwin, EM_REPLACESEL, 0, (LPARAM) messagebuf);
Somehow Windows stops responding to these messages.
Windows7 has some changes (from previous versions of windows)
in process scheduling
which can mess up lock-less multi-thread apps which are working 100% thread safe on older versions of windows. There is not much to do with this (unless add some safety sleeps or locks)
critical sections
I am not 100% convinced that critical sections work properly on Win7SP1 as I have some issues on specific machine setup where they do not work with combination of:
heavy duty USB HS bulk data transfers
OpenGL use
multi-threading with heavy multiple critical sections use
but it could be a hidden bug or messed-up Windows7 installation or even HW error
WindowProc
if your message loop get stuck for a longer time then it should then it will cause the program to be detected as non-responding even if it is not. Usually run in compatibility for XPSP3 helps but sadly not on all machines or all apps :( this is the main reason why many games does not run on Win7
I suspect this is your case and according to your last comment I was right.
Transfer critical processing to some thread instead so the WindowProc is not blocked. If you need an event after computation is done then flag some variable as done and scan for it in some timer so you can respond inside main thread (to avoid winapi calls outside main thread).
32bit Driver+App use on x64 windows (WOW64)
if your app is accessing any driver then on WOW64 you need special driver build to access real hardware instead of emulated by WOW64 !!! If you did not then your App is most likely waiting for real device respond instead is getting emulated one which can be different and cause real hang ups. In that case you need compile your app as x64
Or use some kind of WOW64.x86 -> x64 bridge
Or use driver which can handle it itself (usually link do different dll)
Related
I have been working for 2.5 years on a personal flight sim project on my leisure time, written in C++ and using Opengl on a windows 7 PC.
I recently had to move to windows 10. Hardware is exactly the same. I reinstalled Code::blocks.
It turns out that on first launch of my project after the system start, performance is OK, similar to what I used to see with windows 7. But, the second, third, and all next launches give me lower performance, with significant less fluidity in frame rate compared to the first run, detectable by eye. This never happened with windows 7.
Any time I start my system, first run is fast, next ones are slower.
I had a look at the task manager while doing some runs. The first run is handled by one of the 4 cores of my CPU (iCore5-6500) at approximately 85%. For the next runs, the load is spread accross the 4 cores. During those slower runs on 4 cores, I tried to modify the affinity and direct my program to only one core without significant improvement in performance. The selected core was working at full load, though.
My C++ code doesn't explicitly use any thread function at this stage. From my modest programmer's point of view, there is only one main thread run in the main(). On the task manager, I can see that some 10 to 14 threads are alive when my program runs. I guess (wrongly?) that they are implicitly created by the use of joysticks, track ir or other communication task with GPU...
Could it come from memory not being correctly freed when my program stops? I thought windows would free it properly, even if I forgot some 'delete' after using 'new'.
Has anyone encountered a similar situation? Any explanation coming to your minds?
Any suggestion to better understand these facts? Obviously, my ultimate goal is to have a consistent performance level whatever the number of launches.
trying to upload screenshots of second run as viewed by task manager:
trying to upload screenshots of first run as viewed by task manager:
Well I got a problems when switching to win10 for clients at my work too here few I encountered all because Windows10 has changed up scheduling of processes creating a lot of issues like:
older windowses blockless thread synchronizations techniques not working anymore
well placed Sleep() helps sometimes. Btw. similar problems was encountered when switching from w2k to wxp.
huge slowdowns and frequent freezes for few seconds on older single threaded apps
usually setting affinity to single core solves this. You can do this also in task manager just to check and if helps then you can do this in code too. Here an example on how to do it with winapi:
Cache size estimation on your system?
messed up drivers timings causing zombies processes even total freeze and or BSOD
I deal with USB in my work and its a nightmare sometimes on win10. On top of all this Win10 tends to enforce wrong drivers on devices (like gfx cards, custom USB systems etc ...)
auto freeze close app if it does not respond the wndproc in time
In Windows10 the timeout is much much smaller than in older versions. If the case You can try running in compatibility mode (set in icon properties on desktop) for older windows (however does not work for #1,#2), or change the apps code to speed up response. For example in VCL you can call Process Messages from inside of blocking code to remedy this... Or you can use threads for the heavy lifting ... just be careful with rendering and using winapi as accessing some winapi (any window/visual related stuff) functions from outside main thread causes havoc ...
On top of all this old IDEs (especially for MCUs) don't work properly anymore and new ones are usually much worse (or unusable because of lack of functionality that was present in older versions) to work with so I stayed faith full to Windows7 for developer purposes.
If none of above helps then try to log the times some of your task did need ... it might show you which part of code is the problem. I usually do this using timing graph like this:
both x,y axises are time and each task has its own color and row in graph. the graph is scrolling in time (to the left side in my case) and has changeable time scale. The numbers are showing actual and max (or sliding avg) value ...
This way I can see if some task is not taking too much time or even overlaps its next execution, peaks are also nicely visible and all runs during runtime without any debug tools which might change the behavior of execution.
We have a bigger software running on Win CE6 without problems. The core functionality is implemented in a COM server DLL that provides connection points. The COM client program registers event handlers for the connection points on program startup to get status notifications etc. On program exit it unregisters the handlers by calling the corresponding IConnectionPointImpl::Unadvise methods.
Now, we are porting the program to run on Win EC 7. The new Board Support Package (BSP) for Win EC 7 works well. There are also different versions with different options, created at different times with different sources from Microsoft, but our software always show the same issue.
On program startup, ~10s after launch, IConnectionPointImpl::Unadvise is called unexpectedly on all registered event handlers. We only have one method in our source code that calls IConnectionPointImpl::Unadvise and this is definitely not executed.
The issue appears ~95%, but sometimes the program starts and runs without problems. We cannot use the Debugger because of the size of the program, the performance is very poor.
We guess, that the COM runtime calls the IConnectionPointImpl::Unadvise methods for some reasons. But we have no idea, how to prevent this.
Has anybody observed the same issue? Is there a solution/workaround available? Thanks.
So we finally found how solve this problem.
We remove our dependency on MarshalByReObject and replace it by a proper implementation of ISerializable.
That allow us to load properly inside custom AppDomain our assembly and events are not loose anymore.
But this has a side effect on path where assembly a configuration file are loaded. To solve this we also implement an AppDomain.AssemblyResolve event which allow us to redirect the loading in a proper place.
I hope this can help you ;)
Today I ported my old memory benchmark
from Borland C++ builder 5.0 to BDS2006 Turbo C++ and found out weird thing.
exe from BCB5 runs OK and stable
exe from BDS2006 measure OK only before main Form is started (inside its constructor) and if the benchmark is started again after main form is Activated or even after any VCL component change (for example Caption of main form) then the speed of benchmark thread is strongly affected.
After some research I found out that:
Does not mater if test is inside thread or not.
The process/thread priority,affinity does not affect this either.
Hide of any window (Visibility,Enabled) does not affect this.
call the test form OnIdleEvent does not affect this
does not mater if time is measured by RDTSC or PerformanceCounter
My conclusion is that VCL library runs some code/thread on the background so my questions are:
Is there a way to temporarily pause VCL code/stuff ?
ideal something like Application->Pause(); and Application->Resume(); or just Forms.
what else could cause this behavior and how to avoid it ?
PS.
Test application has no VCL components other than main form. Benchmark is just a few memory transfers by rep stosd with different block sizes (no funny stuff). Source is in this related Q/A. I know BDS2006 is out-dated but I am not looking for upgrade right now so please skip any comments about that they are not help-full at all.
Tested on Windows7 pro x64, 32bit Application
I found out that wndproc in BDS2006::VCL invalidates CACHEs.
I have tried to override wndproc by winapi
for Application->Handle is this easy but it does not stop the processing of messages for Form. When I tried Form1->Handle as window then error 1400 occurs (not valid window handle)
I have tried to override wndproc by VCL
for Application by TApplication events and for Form by override of virtual wndproc member. Message processing stops but their calling sequences remains and the problem is not solved either.
So my conclusion after eliminating every possibility I can think off is that I need to flush CACHE more intensively somehow after setting process/thread for benchmarking.
In DOS I would done it by single instruction but on windows it is more tricky. Well The previous version of memory benchmark used just memory filling which is obviously not enough for BDS2006 exe. I think that instruction CACHE is involved in this problem not data cache so I change it a bit and it finally worked thing out.
Flushing the CPU CACHE:
for (DWORD i=0;i<(128<<20);i+=7)
{
dat[i]+=i;
dat[i]*=i;
dat[i]&=i;
}
Where dat is 128MB allocated memory chunk (or bigger) and must be done after all process/thread priority and affinity changes or all winapi calls prior to benchmarking.
I'm working on an application that uses multiple threads to process its data. The app is developped in C++ (Intel C++ comp. 9.1) and uses OpenMP. It is a 64 bit app running on Win7.
The problem is that when I run it during day, it runs correctly. But when I run it during night after the screen has been locked, it enters in a forever loop after a few processes.
To be more precise, the app is called many times for different files to process. The calls are done within a batch file (no problem there).
I found that it enters in the forever loop about 2 hours after the lock screen occurs.
I disabled all power saving settings. But nothing changed.
It is not very clear as description but the reason is that I don't have a clue about the source of the problem. I just hope someone among you could have had the same problem (and found a fix!). If you want more details, just let me know.
Any idea? Thanks in advance!
As my tests go on, I installed the same setup (but in release rather than debug version) on another computer. I ran into the same problem after 20 minutes (after the screen lock) with another set of data. I ran the same data on my own computer (which is not locked) and everything was fine.
I'm mystified!
Are you giving a thread priority that is taking control of the application?
Also, I would suggest taking running it through some kind of profiling, such as VTune as it can point out potential odd cases that could be causing an issue for you. (There is a free evaluation that you can try).
This is something that's been bothering me a while and there just has to be a solution to this. Every time I call ShellExecute to open an external file (be it a document, executable or a URL) this causes a very long lockup in my program before ShellExecute spawns the new process and returns. Does anyone know how to solve or work around this?
EDIT: And as the tags might indicate, this is on Win32 using C++.
I don't know what is causing it, but Mark Russinovich (of sysinternal's fame) has a really great blog where he explains how to debug these kinds of things. A good one to look at for you would be The Case of the Delayed Windows Vista File Open Dialogs, where he debugged a similar issue using only process explorer (it turned out to be a problem accessing the domain). You can of course do similar things using a regular windows debugger.
You problem is probably not the same as his, but using these techniques may help you get closer to the source of the problem. I suggest invoking the CreateProcess call and then capturing a few stack traces and seeing where it appears to be hung.
The Case of the Process Startup Delays might be even more relevant for you.
Are you multithreaded?
I've seen issues with opening files with ShellExecute. Not executables, but files associated an application - usually MS Office. Applications that used DDE to open their files did some of broadcast of a message to all threads in all (well, I don't know if it was all...) programs. Since I wasn't pumping messages in worker threads in my application I'd hang the shell (and the opening of the file) for some time. It eventually timed out waiting for me to process the message and the application would launch and open the file.
I recall using PeekMessage in a loop to just remove messages in the queue for that worker thread. I always assumed there was a way to avoid this in another way, maybe create the thread differently as to never be the target of messages?
Update
It must have not just been any thread that was doing this but one servicing a window. Raymond (link 1) knows all (link 2). I bet either CoInitialize (single threaded apartment) or something in MFC created a hidden window for the thread.