Debugger hanging at program start (Loader lock related?)

I'm developing a C# MDX application. Sometimes (maybe 1 out of 5 times) when I debug the application (by pressing F5) it hangs before displaying the main window. I'm sure it's happening before DirectX gets setup, but I'm not sure where - because the debugger hangs too. I notice that if I press F10 (step in) followed by F5 (run) the problem never happens and I also notice that it happens more often on slower computers than faster ones. I'm guessing it's a race condition, and it has something to do with the Loader Lock exception that I disabled in order to use MDX.
Putting the following code at the very beginning of Main seems to reduce the probability of the problem occurring (but it still happens):
#if DEBUG
System.Threading.Thread.Sleep(500);
#endif
Is this happening to anyone else Does anyone know why this happens, or how to fix it
-Jeremy


Answer this question

Debugger hanging at program start (Loader lock related?)

  • Cla82

    > Does it happen if you run the SDK samples

    How did I know you were going to ask that :)

    It doesn't appear to, but those applications are small and don't use WinForms. So I'm not sure it means anything that they don't.

    Two mistakes in my above post. The Sleep doesn't appear to help after all. And it does sometimes happen when I hit F10 (although not as often, I think). I'm assuming none of my own code gets executed before the debugger hits the first line of Main. Am I wrong If so, then it couldn't be anything I'm doing to cause it. Or am I wrong on that point also

    This has happened periodically for years now (even when I was using Visual Studio 7.0). I thought the problem went away when I upgraded to .NET 2.0, but it now it's happening more and more often and is becoming very annoying. I've never seen this problem in a release or when I'm running without the debugger.

    Any other suggestions Format hard drive and start over

    -Jeremy





  • Ricky Tan - MSFT

    Sounds pretty odd to me. I've run hundreds of c# MDX apps since VS05 came out and I've never seen this. Does it happen if you run the SDK samples

    Do you have any multithreading going on in your game

    Is this only happening if the debugger is attached (try ctrl-f5 rather than f5).



  • Snyper_Vash

    I always assumed it was MDX because I've never seen it happen in my other applications. Also, disabling "LoaderLock" didn't exactly fill me with confidence.

    Your "static constructors" comment got me thinking about how the JIT would load the modules and start the program running. I added these lines to the beginning of Main:

    Microsoft.DirectX.Vector3 v = new Microsoft.DirectX.Vector3();
    Microsoft.DirectX.Direct3D.Mesh m = null;

    I've run both projects a bunch of times, and it never hung once. (However, this problem has disappeared for days at a time, so I can't be sure for a while.)

    -Jeremy


  • Jan Kučera

    If it happens before you hit a line of your code then it would seem unlikley to be realted to the MDX and loader lock. No MDX code has yet run at that point other than any static constructors (which get run as soon as a class which references that object is JIT'd).

    The only suggestions would be turning on DirectX debugging and seeing if there is anything visible there - don't forget to enable unmanaged debugging. Or learn how to use the kernal debugger and break into your app and see what ts doing (no I don't have a clue how to do that).

    Finally - debugging via // - yes start commenting out blokcs of code until it stops. You know an empty project doesn't do it and your project does so work backwards. Though this might be slower than rebuilding the machine. Do you see this problem on other machines

    You didn't say if this happens with ctrl-f5 or not - or if you run the EXE on other machines.



  • vbnetdiscuss

    Ok, it still happens but less often. Maybe 1 out of 5 times (although it did go away completely for a day or two).

    When the debugger is stuck, I get the following from the task manager:

    Mem usage = 14,300K
    VM Size = 21,868K

    When I hit F10 (start debugging, stopped at the first line of main - none of my own static constructors were executed) I get this:

    Mem usage = 14,660K
    VM Size = 22,968K

    So I guess the debugger is hanging just before the first line of main. I don't think there's anything I'm doing to cause it (but even so, it still shouldn't hang the debugger) - unless it has something to do with LoaderLock. It's very intermittent, but I've seen it happen on two different computers, so it's probably not corrupted software or hardware.

    I thought it was related to MDX but it might not be. So this is probably the wrong forum to discuss this problem. Sorry.

    Kernel level debugger, eh

    -Jeremy



  • Debugger hanging at program start (Loader lock related?)