Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Dear CPians: Help me out by voting for this

Dear CPians: Help me out by voting for this

Scheduled Pinned Locked Moved The Lounge
helpcsharpdatabasecomdebugging
52 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rama Krishna Vavilala

    I wrote a test application to validate my theory and it works. Of course, it can be improved a lot. The idea is to get the dll name and the address at which the access violation occured. I wrote a C++ dll called Crasher with a method that causes access violation:

    extern "C" _declspec(dllexport) int _stdcall CrashMyApp()
    {
    char* sz = 0;
    *sz = 0;

    return -1;
    

    }

    Now I call this from a managed assemvbly and catch access violation and get more information:

    static void Main(string[] args)
    {
    try
    {
    CrashMyApp();
    }
    catch (AccessViolationException)
    {
    IntPtr ex = Marshal.GetExceptionPointers();

        EXCEPTION\_POINTERS pointers = (EXCEPTION\_POINTERS)Marshal.PtrToStructure(ex, typeof(EXCEPTION\_POINTERS));
        EXCEPTION\_RECORD rec = (EXCEPTION\_RECORD)Marshal.PtrToStructure(pointers.ExceptionRecord, typeof(EXCEPTION\_RECORD));
        Console.WriteLine("Exception in {0} at {1:x8}", DllNameFromAddress(rec.ExceptionAddress), rec.ExceptionAddress.ToInt32());
    
        // Usually you wil re-throw may be wrapping in some other exception
        // Bad idea to catch AccessViolationException and do nothing
    }
    

    }

    All the utility methods are in the project download link at the bottom of the post. So the output is like this:

    Exception in C:\Users\ramakrishna\Documents\Visual Studio 2008\Projects\UnmanagedDebugging\Debug\Crasher.dll at 77b71328

    Download link here: Crasher.zip (8.5 KB) Of course, you can build a lot fancy stuff like load the symbols, generate mini-dumps etc.

    D Offline
    D Offline
    Daniel Vaughan
    wrote on last edited by
    #40

    Nice one Rama!

    Daniel Vaughan Blog: DanielVaughan.Orpius.com
    Company: Outcoder

    1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      Probably it might be easier to put the code in an event handler for Application.ThreadException. That way you don't have to add individual try-catch blocks.

      I Offline
      I Offline
      IncredibleMouse
      wrote on last edited by
      #41

      Probably even easier still to go to the beach with your family while MS does their danged jobs. ;P

      J 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Dear CPians, Have you ever written a .NET desktop or web application that talks to a SQL database? Uses WinForms controls? Talks to COM objects? Talks to Win32 via P/Invoke? If so, you may have run into the dreaded AccessViolationException: Some managed code called into unmanaged code, and memory was corrupted. Maybe you passed a bad argument to the unmanaged function. Maybe there's a bug in the managed-to-native interop. Whatever the case, when it happens, all you get is an unhelpful message and a useless stack trace, making it near-impossible to debug. Please vote up this MSConnect case[^] so Microsoft gives us more information when these errors occur. If MS gives us more information, devs can fix crashing .NET apps, users will be happier, and the world will be a better place. Thank you.

        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

        I Offline
        I Offline
        IncredibleMouse
        wrote on last edited by
        #42

        +1

        J 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          I wrote a test application to validate my theory and it works. Of course, it can be improved a lot. The idea is to get the dll name and the address at which the access violation occured. I wrote a C++ dll called Crasher with a method that causes access violation:

          extern "C" _declspec(dllexport) int _stdcall CrashMyApp()
          {
          char* sz = 0;
          *sz = 0;

          return -1;
          

          }

          Now I call this from a managed assemvbly and catch access violation and get more information:

          static void Main(string[] args)
          {
          try
          {
          CrashMyApp();
          }
          catch (AccessViolationException)
          {
          IntPtr ex = Marshal.GetExceptionPointers();

              EXCEPTION\_POINTERS pointers = (EXCEPTION\_POINTERS)Marshal.PtrToStructure(ex, typeof(EXCEPTION\_POINTERS));
              EXCEPTION\_RECORD rec = (EXCEPTION\_RECORD)Marshal.PtrToStructure(pointers.ExceptionRecord, typeof(EXCEPTION\_RECORD));
              Console.WriteLine("Exception in {0} at {1:x8}", DllNameFromAddress(rec.ExceptionAddress), rec.ExceptionAddress.ToInt32());
          
              // Usually you wil re-throw may be wrapping in some other exception
              // Bad idea to catch AccessViolationException and do nothing
          }
          

          }

          All the utility methods are in the project download link at the bottom of the post. So the output is like this:

          Exception in C:\Users\ramakrishna\Documents\Visual Studio 2008\Projects\UnmanagedDebugging\Debug\Crasher.dll at 77b71328

          Download link here: Crasher.zip (8.5 KB) Of course, you can build a lot fancy stuff like load the symbols, generate mini-dumps etc.

          A Offline
          A Offline
          aquatarian
          wrote on last edited by
          #43

          :thumbsup: very useful and thanks also to the gentleman suggesting an event handler approach; may code be forever cleaner and more readable with error handling out of the way :-D

          1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Dear CPians, Have you ever written a .NET desktop or web application that talks to a SQL database? Uses WinForms controls? Talks to COM objects? Talks to Win32 via P/Invoke? If so, you may have run into the dreaded AccessViolationException: Some managed code called into unmanaged code, and memory was corrupted. Maybe you passed a bad argument to the unmanaged function. Maybe there's a bug in the managed-to-native interop. Whatever the case, when it happens, all you get is an unhelpful message and a useless stack trace, making it near-impossible to debug. Please vote up this MSConnect case[^] so Microsoft gives us more information when these errors occur. If MS gives us more information, devs can fix crashing .NET apps, users will be happier, and the world will be a better place. Thank you.

            Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

            E Offline
            E Offline
            Eric Whitmore
            wrote on last edited by
            #44

            I did my part.

            PlutoX

            J 1 Reply Last reply
            0
            • B BillWoodruff

              Hi Judah, Vote++; 12:50PM (GMT +7) best, Bill

              "Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #45

              Thanks, Bill!

              Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

              1 Reply Last reply
              0
              • I IncredibleMouse

                +1

                J Offline
                J Offline
                Judah Gabriel Himango
                wrote on last edited by
                #46

                Thanks!

                Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                1 Reply Last reply
                0
                • S Shamit Kumar Tomar

                  Voted it up. Well, due to these arbitrary difficulties in VC++/.NET, I over the years switched to PHP/MySQL after completing my span of being a workplace hero in Windows environment for half a decade. And I must say, I am much more happy and productive in PHP/MySQL environment :laugh: but seriously miss the challenging and real development of VC++ days. :(( - Pragmatic Programmer

                  J Offline
                  J Offline
                  Judah Gabriel Himango
                  wrote on last edited by
                  #47

                  Thanks, Shamit!

                  Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                  1 Reply Last reply
                  0
                  • E Eric Whitmore

                    I did my part.

                    PlutoX

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #48

                    Thank you!

                    Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                    1 Reply Last reply
                    0
                    • J Judah Gabriel Himango

                      Dear CPians, Have you ever written a .NET desktop or web application that talks to a SQL database? Uses WinForms controls? Talks to COM objects? Talks to Win32 via P/Invoke? If so, you may have run into the dreaded AccessViolationException: Some managed code called into unmanaged code, and memory was corrupted. Maybe you passed a bad argument to the unmanaged function. Maybe there's a bug in the managed-to-native interop. Whatever the case, when it happens, all you get is an unhelpful message and a useless stack trace, making it near-impossible to debug. Please vote up this MSConnect case[^] so Microsoft gives us more information when these errors occur. If MS gives us more information, devs can fix crashing .NET apps, users will be happier, and the world will be a better place. Thank you.

                      Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                      G Offline
                      G Offline
                      GuyWithDogs
                      wrote on last edited by
                      #49

                      Added my vote ...

                      J 1 Reply Last reply
                      0
                      • G GuyWithDogs

                        Added my vote ...

                        J Offline
                        J Offline
                        Judah Gabriel Himango
                        wrote on last edited by
                        #50

                        Thank you, dog lover!

                        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                        1 Reply Last reply
                        0
                        • I IncredibleMouse

                          Probably even easier still to go to the beach with your family while MS does their danged jobs. ;P

                          J Offline
                          J Offline
                          Judah Gabriel Himango
                          wrote on last edited by
                          #51

                          :laugh:

                          Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                          1 Reply Last reply
                          0
                          • D Dan Neely

                            Should this be posted on connect as a workaround? :confused:

                            The latest nation. Procrastination.

                            J Offline
                            J Offline
                            Judah Gabriel Himango
                            wrote on last edited by
                            #52

                            Done, with rockstar Rama getting full credit.

                            Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups