> Nevertheless, I just tried this in VS.Net 2003,
Will,
Thanks for your help.
Do these "control87" commands need to be issued on every thread in the app?
Do I need to constantly apply them to guarantee they "stick"? Can other
processes disable them? Can opening a common dialog (such as File | Open)
reset them?
Thanks,
Chris
William DePalo [MVP VC++] - 08 Mar 2005 18:39 GMT
> Do these "control87" commands need to be issued on every thread in the
> app?
As I said, I don't do any "scientific" calculation these days. But as the
exception mechanism of the platform _is thread based_, I think that the
answer is yes.
> Do I need to constantly apply them to guarantee they "stick"?
Good question. I don't know. I would guess that the runtime sets the
exception policy while processing the DLL_THREAD_ATTACH notification and not
thereafter. That's a guess. If you have the runtime source (disclaimer: I
can't say for certain how much is distributed with the VS.Net IDE) you might
want to take a look.
> Can other processes disable them?
I can't think of an easy way.
> Can opening a common dialog (such as File | Open) reset them?
Well, the thing to note about exception handlers under Win32 is that they
are stack-frame based (tabling for now the issue of "vectored" exception
handing). So, if you call on a common dialog and if it does something like
this
__try
{
DoTheCommonDialogThing();
}
__except ( blah blah blah ... )
{
// swallow the exception and move on
}
then you (the caller) will never see it.
Regards,
Will
Jan Bares - 09 Mar 2005 08:10 GMT
> processes disable them? Can opening a common dialog (such as File | Open)
> reset them?
Hi,
just add to previous answers, some device drivers (e.g. HP printer drivers)
mask FPU exceptions and do not restore them back. So it can happen in your
program, that after call to some API (3rd party DLL or so), the exceptions
will be masked. On the contrary, some 3rd party DLL may not work due to
unmasked exceptions. (We unmasked FPU exceptions in AutoCAD 2k, and it won't
even start due to divion by zero)
Jan
> The last two lines raise exceptions in the debugger.
Ahh... I solved the problem. I had the bits inverted in the first argument
to the _control87 function.
This raised a second problem (pun not intended). When an invalid floating
point operation occurs and is caught by correct use of _control87, the call
stack gets hosed. I cannot see where the exception happened in my code!
It appears the floating point error jumps the code into the kernel and blows
the call stack. :(
Anyone know how I can track down these floating point errors?
Thanks again for your help,
Chris
Chris Stankevitz - 08 Mar 2005 18:17 GMT
> It appears the floating point error jumps the code into the kernel and
> blows the call stack. :(
This example illustrates the point. Create a console app and set exceptions
as shown here:
http://www.stankevitz.com/visualstudio/exceptions.png
Then compile/run this code:
//--------------------
#include <cfloat>
#include <cmath>
int main()
{
_control87(0, _MCW_EM);
sqrt(-1.0);
return 0;
}
//--------------------
Thanks,
Chris
Chris Stankevitz - 08 Mar 2005 18:44 GMT
>> It appears the floating point error jumps the code into the kernel and
>> blows the call stack. :(
Turns out the call stack comes back if you "pass exception on the caller" a
few times.
Okay, my problem is solved, thanks for your help everyone!
Chris
Doug Harrison [MVP] - 08 Mar 2005 21:11 GMT
>Turns out the call stack comes back if you "pass exception on the caller" a
>few times.
>
>Okay, my problem is solved, thanks for your help everyone!
One more thing to consider is that FPU exceptions are somewhat asynchronous
and may not be raised until a subsequent FPU instruction is executed. You
can raise any pending FPU exceptions with:
__asm { fwait }
I just found an an article on MSDN which mentions this:
Microsoft Visual C++ Floating-Point Optimization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/ht
ml/floapoint.asp
I haven't had a chance to read this article, but it looks very good.

Signature
Doug Harrison
Microsoft MVP - Visual C++
William DePalo [MVP VC++] - 08 Mar 2005 18:48 GMT
> This raised a second problem (pun not intended).
:-)
> When an invalid floating point operation occurs and is caught by correct
> use of _control87, the call stack gets hosed. I cannot see where the
> exception happened in my code!
Does your exception handler call _clearfp()? I think that that is required
to clear the exception. I'm wondering whether your problem is that the
exception is signalled mre times than you think because it has not been
cleared.
As I said before in this thread, that's just a guess.
Regards,
Will