1

We have a problem with an application crashing when it calls a method that uses Double.MaxValue. This is true running in the debugger as well as in the release version, and happens under 32-bit XP as well as under 64-bit Win 7.

If in the debugger I have a breakpoint on a line calling a method using Double.MaxValue, any attempt to execute that method (F11, F10, F5) will stop the application with the friendly windows message that it has caused a problem and needs to be terminated.

Replacing Double.MaxValue with 9999999999 - which is ok for our application - makes the problem go away. Looking at the IL, literally all that changes is the constant.

The C# classes are being called from native code via a little C++ mixed mode bridge that has a native and a managed part. Our first idea was that the native part might have destroyed managed memory areas - but after replacing MaxValue with 9999999999 the application runs fine, and happily jumps back and forth between native and managed part.

We don't like to deliver trial & error software to the customer, and would love to hear any ideas about what could be going on??

--EDIT--

Here is the most cut-down version of the bridge that still crashes:

#include <stdio.h>
#include <tchar.h>
#include <vcclr.h>

// -------------------------------------------------------------
//  Managed
// -------------------------------------------------------------

#pragma managed

using namespace System;
using namespace OurCompany::Tools;

// ACtion Move text window
__int32 AktionTextfensterMove (__int32 i32HNCTeil, __int32 i32Ansicht, void* pvoid)
{
   return GUEManagedWrapper::TextfensterVerschieben (i32HNCTeil, i32Ansicht, nullptr);
}

// -------------------------------------------------------------
//  Native
// -------------------------------------------------------------

#pragma unmanaged

#define DLLExport __declspec(dllexport)

extern "C" 
{
// Action Move text window
DLLExport __int32 GUENMBridgeTextfensterMove (__int32 i32HNCTeil, __int32 i32Ansicht,         void* pvoid)
{
  return AktionTextfensterMove (i32HNCTeil, i32Ansicht, pvoid);

} }

---EDIT 2---

...and here is the C# code:

public static class GUEManagedWrapper
{
  public static Int32 TextfensterVerschieben (Int32 i32HNCTeil, Int32 i32Ansicht, FizzBuzz fzzBzz)
  {
     fzzBzz= new FizzBuzz();
     fzzBzz.fd  = Double.MaxValue;
     return 0;
  }
}

public class FizzBuzz
{
    public Double fd;
}

This crashes; replacing Double.MaxValue with 9999999999 fixes the crash.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
Joel in Gö
  • 7,460
  • 9
  • 47
  • 77
  • 2
    can you show A SSCCE (Short, Self Contained, Correct (Compilable), Example) that exhibits the behaviour? – Mitch Wheat May 13 '13 at 07:55
  • 2
    Is it really `Double.MaxValue` that's causing the crash? I would suspect it's the function you call that's crashing because it doesn't handle that particular value. – Dirk May 13 '13 at 08:01
  • @Dirk: Yes, we are sure. The value is not handled anywhere else, it is just the start value for a mathematical calculation : Double min = Double.MaxValue, ... min = Math.Min(min, newDouble) ... – Joel in Gö May 13 '13 at 08:04
  • You could try attaching a native mode debugger to your process to debug the C++ method. (I don't have much experience using mixed mode assemblies so I don't know if that works well) – Dirk May 13 '13 at 08:05
  • 2
    "Any attempt to make a trivial example does not crash," - That's rarely the case. Surely you can show some code?' – Mitch Wheat May 13 '13 at 08:05
  • Correction: my colleague cut down the brige - see update. – Joel in Gö May 13 '13 at 08:20
  • ...and the C# code :) – Joel in Gö May 13 '13 at 08:39
  • I don't think this is compilable code. Where is the definition of the type TextfensterVerschiebenParas? – Daniel Daranas May 13 '13 at 08:42
  • TextfensterVerschiebenParas is a class holding only fields. – Joel in Gö May 13 '13 at 08:51
  • In any case, the code is not compilable. That class is precisely where the crash is, so I think it's important to also have its definition. – Daniel Daranas May 13 '13 at 08:58
  • Ok, fixed. Names slightly changed to help the eye strain of LongGermanMethodAndClassnames... – Joel in Gö May 13 '13 at 08:59
  • This is a mystery. It seems to have happened before ( http://stackoverflow.com/questions/5095116/strange-error-when-using-double-nan-and-double-maxvalue-in-c-sharp ), but still... – Daniel Daranas May 13 '13 at 09:24
  • This question is too poorly documented, it isn't even clear whether it is the debugger or Visual Studio that crashes. If you can't document it better with a good SSCCE example then you'll need the help from Microsoft Support. You can send them a copy of your project. – Hans Passant May 13 '13 at 12:41
  • Visual Studio is the debugger; however, that is not the problem. The program crashes if you debug it in VS2008 (on entering the method containing the Double.MaxValue), but also if you compile it and run the .exe file. – Joel in Gö May 13 '13 at 13:50
  • What additional documentation do you need? – Joel in Gö May 13 '13 at 13:53
  • Does a check on your EXE with .NET Reflector (http://en.wikipedia.org/wiki/.NET_Reflector) give correct output? About checking dependencies, see also this question ( http://stackoverflow.com/questions/197447/how-to-find-all-dependencies-of-a-net-project ). – Daniel Daranas May 13 '13 at 15:18
  • Does anything change if you debug and in the Exceptions option of Visual Studio mark all the "break when exception is thrown"? – Daniel Daranas May 13 '13 at 15:19
  • Does the "minimal" code you posted crash in all machines? Did you test in more than one pc? – Daniel Daranas May 13 '13 at 15:23
  • Does the crash also happen if you replace Double.MaxValue with the _actual_ literal value of Double.MaxValue, instead of 9999999999? Does it crash with 1E20? 1E40? – Daniel Daranas May 13 '13 at 15:24
  • Thanks for the ideas - will check and get back to you! – Joel in Gö May 13 '13 at 15:31
  • Does the behavior change when you pass in a variable to GUEManagedWrapper::TextfensterVerschieben (i32HNCTeil, i32Ansicht, nullptr); instead of a nullptr? TextFensterVerschieben is obviously made to return a new FizzBuzz instance. – Mike Lischke May 15 '13 at 08:43

1 Answers1

0

I had the same problem. Visual Express 2010 c#

double afunction() {
   Console.WriteLine("afunction called");
   if (condition) { return somedoublevalue; }
   ....
   if (condition) { return double.maxValue; }
   return 0;
} 

crashed with a StackOverFlow, whenever afunction() was called (before the Console message). replacing double.maxValue by any double literal solved the problem

afl
  • 1