[9.0rc5] Invalid floating point operation exception

Bug reports will be moved here when the described bug has been fixed

Moderators: white, Hacker, petermad, Stefan2

User avatar
MarcinW
Power Member
Power Member
Posts: 852
Joined: 2012-01-23, 15:58 UTC
Location: Poland

Post by *MarcinW »

Getting or not getting an exception may depend on codecs, installed in the system.

I faced a similar problem when calling some AVICAP API functions. They internally use codecs installed in the system, and some codecs use MMX instructions and leave the FPU in an inconsistent state in case of an internal codec error.

I created the following solution, that worked for me:

Code: Select all

// An example of calling capGetDriverDescription API, that may call flawed codecs
try
  Result:=capGetDriverDescription(DriverNumber,Name,Length(Name),Version,Length(Version));
finally
  FpuInit;
end;
Where FpuInit is:

Code: Select all

procedure FpuInit;
{$IFOPT W+}
{$DEFINE STACKFRAMESON}
{$ENDIF}
{$W-}

{$IFDEF CPUX64}
  function DefaultMXCSRAddr : Pointer; {Pascal function is to allow building with packages}
  begin
    Result:=@DefaultMXCSR;
  end;
{$ELSE}
  function Default8087CWAddr : Pointer; {Pascal function is to allow building with packages}
  begin
    Result:=@Default8087CW;
  end;
{$ENDIF}

{$IFDEF STACKFRAMESON}
{$UNDEF STACKFRAMESON}
{$W+}
{$ENDIF}
asm
{$IFDEF CPUX64}
    PUSH    RAX
    CALL    DefaultMXCSRAddr
    LDMXCSR [RAX]
    POP     RAX
{$ELSE}
    FNINIT
    FWAIT
    PUSH    EAX
    CALL    Default8087CWAddr
    FLDCW   [EAX]
    POP     EAX
{$ENDIF}
end;
Regards
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

Perhaps this may have sense sometimes, I doubt that any system codecs are used in out particular case when we are talking about EXIF decoding with non-system application module.
User avatar
MarcinW
Power Member
Power Member
Posts: 852
Joined: 2012-01-23, 15:58 UTC
Location: Poland

Post by *MarcinW »

I just wanted to say that I faced an "Invalid floating point operation" exception when using codecs, and TC faces it when using some other external component.

In both cases it's the same exception raised in our applications, but caused by an external code, so there is a chance, that FpuInit may help in TC's case.

----

Side note: a bit similar problem may occur when loading DLLs. Some of DLLs - in particular from Microsoft - reconfigure FPU in their initialization code. There is a SafeLoadLibrary function in Delphi, that restores FPU configuration after DLL loading.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post by *ghisler(Author) »

Well, I'm already doing such floating point init calls in TC before calling certain functions, e.g. for the context menu.

But as I wrote, I cannot do that for the Lister plugins, because they are calling functions themselves from their own window.
Author of Total Commander
https://www.ghisler.com
User avatar
MarcinW
Power Member
Power Member
Posts: 852
Joined: 2012-01-23, 15:58 UTC
Location: Poland

Post by *MarcinW »

I think there is a possible solution: subclass the plugin window with something like this:

Code: Select all

function SubclassedWindowProc(Window : HWND; MessageCode : LongWord; wParam : WPARAM; lParam : LPARAM) : LRESULT; stdcall;
var
  OrigWindowProc : Pointer;
  S : Single;
begin
  OrigWindowProc:=Pointer(GetWindowLong(Window,GWL_USERDATA));
  if Assigned(OrigWindowProc) then
    Result:=CallWindowProc(OrigWindowProc,Window,MessageCode,wParam,lParam)
  else
    Result:=DefWindowProc(Window,MessageCode,wParam,lParam);
  try
    {Do some FPU math, so an exception will be raised in case of invalid FPU state}
    S:=0;
    if S+1 = 0 then;
  except
    FpuInit;
  end;
end;
In practice, there will be no slow down in case of normal FPU state.
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post by *ghisler(Author) »

TC does this already, but plugins may generate multiple nested windows etc.
Author of Total Commander
https://www.ghisler.com
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48021
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post by *ghisler(Author) »

I'm now resetting the floating point exceptions also when loading Lister plugins (TC9RC6). This seems to have fixed the Imagine bug for me! Please try it.
Author of Total Commander
https://www.ghisler.com
User avatar
petermad
Power Member
Power Member
Posts: 14739
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Post by *petermad »

I can confirm that I no longer get the invalid floating point exeption with TC9.0rc6 under Windows XP 32bit (the only OS where I could reproduce the bug) :-)
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50b4 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Ovg
Power Member
Power Member
Posts: 756
Joined: 2014-01-06, 16:26 UTC

Post by *Ovg »

Same as petermad
It's impossible to lead us astray for we don't care even to choose the way.
#259941, TC 11.01 x64, Windows 7 SP1 x64
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

Yes, I confirm too that EXIF is shown file in Imagine now! Thanks! :)
Post Reply