14

I am using Tamas Szekeres builds of GDAL including the C# bindings in a desktop GIS application using C# and .net 4.0

I am including the entire GDAL distribution in a sub-directory of my executable with the following folder structure:

\Plugins\GDAL
\Plugins\GDAL\gdal
\Plugins\GDAL\gdal-data
\Plugins\GDAL\proj

We are using EPSG:4326, and the software is built using 32-bit target since the GDAL C# API is using p/invoke to the 32-bit libraries (could try 64 bit since Tamas provides these, haven't gotten around to it yet).

When I run my application I get the following error

enter image description here

This error typically happens when software tries to access a device that is no longer attached, such as a removable drive. It is not possible to "catch" this exception because it pops up a system dialog.

After dismissing the dialog using any of the buttons, the software continues to execute as designed.

The error occurs the first time I call the following method

OSGeo.OSR.CoordinateTransformation.TransformPoint(double[] inout);

The strange stuff:

  • The error occurs on one, and only one computer (so far)
  • I've run this software in several other computers both 32 and 64 bit without problems
  • The error does not ocurr on the first run after compiling the GDAL shim library I am using, it only occurrs on each subsequent run
  • it happens regardless of release, or debug builds
  • it happens regardless of whether the debugger is attached or not
  • it happens regardless of whether I turn on or off Gdal.UseExceptions or Osr.UseExceptions();
  • disabling removable drives causes the bug to disappear. This is not what I consider a real solution as I will not be able to ask a customer to do this.

I have tried the following:

  • catching the error
  • changing GDAL directories and environment settings
  • changing computers and operating systems: this worked
  • used SysInternals ProcMon to trace what files are being opened with no luck, they all appear to be files that exist
  • I re-built the computer in question when the hard drive failed, to no avail.
  • "cleaning" the registry using CCleaner
  • files in GDAL Directory are unchanged on execution

Assumptions

  • Error is happening in unmanaged code
  • During GDAL initialization, some path is referring to a drive on the computer that is no longer attached.
  • I am also working on the assumption this is limited to a computer configuration error

Configuration

  • Windows 7 Pro
  • Intel Core i7 920 @ 2,67GHz
  • 12.0 GB RAM
  • 64-bit OS
  • Drive C: 120 GB SSD with OS, development (Visual Studio 10), etc
  • Drive D: 1 TB WD 10,000k with data, not being accessed for data.

The Question

I either need a direction to trap the error, or a tool or technique that will allow me to figure out what is causing it. I don't want to release the software with the possibility that some systems will have this behaviour.

4
  • Update 1: Based on the first answer below I looked at the number of files in the GDAL directory, and it is stable in number of files. Update 2: I took my own advice and disabled all removable devices on my system by using the Device Manager. This "solved" the problem! This is not a real solution through, so I am going to leave this question open. How can I trace what piece of software is accessing what removable device?
    – Blue Toque
    Jul 17, 2012 at 4:30
  • Perhaps the method call is causing an exception, so Visual Studio tries to load the original source files of the library, which may have been compiled from a drive that on your PC points to an empty drive? You should be able to see that using ProcMon though.
    – CodeCaster
    Jul 17, 2012 at 12:47
  • Hey! do you need any help in these last 8 hours? lets us know as this seems to be a bit o a PITA Jul 23, 2012 at 13:44
  • Hi everyone; I feel I cannot award the bounty as none of the solutions offered actually finds the bug. Excellent ideas; the one about writing code to hit the button is inventive but definitely a horrible horrible hack. I will let the system award half the bounty using the algorithm outlined here: meta.stackexchange.com/questions/16065/…
    – Blue Toque
    Jul 24, 2012 at 2:56

5 Answers 5

2
+25

I have no experience with this library, but perhaps some fresh eyes might give you a brainwave...

Firstly, WELL WRITTEN QUESTION! Obviously this problem really has you stumped...

Your note about the error not occurring after a rebuild screams out: Does this library generate some kind of state file, in its binary directory, after it runs? If so, it is possible that it is saving incorrect path information into that 'configuration' file, in a misguided attempt to accelerate its next start-up.

Perhaps scan this directory for changes between a 'fresh build' and 'first run'?

At very least you might find a file you can clean up on shut-down to avoid this alert...

HTH

2
  • You can upvote the question if you like, to compensate me for the bounty. I'll check the directory for some sort of file written out during startup. With that in mind, there might be something written to the registry which is a lot harder to intercept. Can anyone recommend a tool to intercept registry writes? I guess SysInternals has one.
    – Blue Toque
    Jul 17, 2012 at 3:00
  • Absolutely SysInternals Proces Monitor technet.microsoft.com/en-us/sysinternals/bb896645
    – Adam
    Jul 17, 2012 at 21:27
2

Maybe you can try this:

  • Run diskmgmt.msc
  • Change the driveletter for Disk 2 (right click) if my assumption that Disk 2 is a Removable Disk is true
  • Run your application
  • If this removes the error, something in the application is referring to the old driveletter
  • It could be in the p/invoked libs
  • Maybe see: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46501 It talks about gcc somehow compiling a driveletter into a binary
2
  • I added a comment last night that I tried this (upvote for that). I disabled all of the external drives. I don't consider this a solution though because I won't be able to tell a customer to permanently disable a drive. Since the library doesn't cause a problem on other computers I'm not sure if it's a drive letter issue.
    – Blue Toque
    Jul 17, 2012 at 17:56
  • It would only occur if the drive has removable storage and only if the storage has been removed. If it's a 'low' driveletter, like 'E', it's not likely to occur on many systems as most people will have the lower letters used up by multiple harddrives/partitions which would not cause the problem. But I admit that it's a long shot. Jul 19, 2012 at 14:37
1

+1 Great question, but It is not possible to "catch"

Its one of these awful solutions that will turn up on DailyWTF in 5 years. But for now it is stored here http://www.pinvoke.net/default.aspx/user32.senddlgitemmessage

using Microsoft.VisualBasic;  //this reference is for the Constants.vbNo;  

public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SendDlgItemMessage(IntPtr hDlg, int nIDDlgItem, uint Msg, UIntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);

// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetDlgItemText(IntPtr hDlg, int nIDDlgItem,[Out] StringBuilder lpString, int nMaxCount);

public void ClickSaveBoxNoButton()
{
    //In this example, we've opened a Notepad instance, entered some text, and clicked the 'X' to close Notepad.
    //Of course we received the 'Do you want to save...' message, and we left it sitting there. Now on to the code...
    //
    //Note: this example also uses API calls to FindWindow, GetDlgItemText, and SetActiveWindow.
    //    You'll have to find those separately.

    //Find the dialog box (no need to find a "parent" first)
    //classname is #32770 (dialog box), dialog box title is Notepad
    IntPtr theDialogBoxHandle; // = null;
    string theDialogBoxClassName = "#32770";
    string theDialogBoxTitle = "Notepad";
    int theDialogItemId = Convert.ToInt32("0xFFFF", 16);
    StringBuilder theDialogTextHolder = new StringBuilder(1000);
    //hardcoding capacity - represents maximum text length
    string theDialogText = string.Empty;
    string textToLookFor = "Do you want to save changes to Untitled?";
    bool isChangeMessage = false;
    IntPtr theNoButtonHandle; // = null;
    int theNoButtonItemId = (int)Constants.vbNo;
    //actual Item ID = 7
    uint theClickMessage = Convert.ToUInt32("0x00F5", 16);
    //= BM_CLICK value
    uint wParam = 0;
    uint lParam = 0;

    //Get a dialog box described by the specified info
    theDialogBoxHandle = FindWindow(theDialogBoxClassName, theDialogBoxTitle);
    //a matching dialog box was found, so continue
    if (theDialogBoxHandle != IntPtr.Zero)
    {

        //then get the text
        GetDlgItemText(theDialogBoxHandle, theDialogItemId, theDialogTextHolder, theDialogTextHolder.Capacity);
        theDialogText = theDialogTextHolder.ToString();

    }

    //Make sure it's the right dialog box, based on the text we got.
    isChangeMessage = Regex.IsMatch(theDialogText, textToLookFor);


    if ((isChangeMessage))
    {
        //Set the dialog box as the active window
        SetActiveWindow(theDialogBoxHandle);

        //And, click the No button
        SendDlgItemMessage(theDialogBoxHandle, theNoButtonItemId, theClickMessage, (System.UIntPtr)wParam, (System.IntPtr)lParam);

    }

}
0

It turns out there was no way to definitely answer this question. I ended up "solving" the problem by figuring out that there was some hardware registered on the system that wasn't present. It is still a mystery to me why, after several years, only GDAL managed to provoke this bug.

I will put the inability to catch this exception down to the idiosyncrasies involved with p/invoke and the hardware error thrown at a very low level on the system.

0

You could add custom error handlers to gdal. This may help:

Link

http://trac.osgeo.org/gdal/ticket/2895

2
  • Hi, it's customary in StackOverflow answers to include a summary of the contents of a link or the highlights that specifically answer the question. The goal of SE sites is to become a resource of knowledge, of answers, for years to come. With a link-only answer, the op must dig through another resource to locate an answer he/she might not be sure about. Most importantly, if your link were to ever break, your answer is useless for anyone who visits this page in the future. Consider making and edit to your answer to add more details. Good luck! Jul 23, 2012 at 13:47
  • Thanks for the GDAL error handlers tip; I neglected to mention that I had already tried that as well.
    – Blue Toque
    Jul 23, 2012 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.