[FWTools] Get System.AccessViolationException on GDAL/OGR C# interface

Duy Lam duylamphuong at gmail.com
Wed Jan 11 08:06:30 EST 2012


I downloaded FWTools for Windows 32 bit at http://fwtools.maptools.org/ and
used dlls in csharp folder in my simple .NET Winform application. I'm
trying to use OGR to convert only location data in MapInfo file to GML.
However, I always gets the error System.AccessViolationException whenever
calling to ExportTo*() methods on Geometry object. But what surprises me is
that the exception doesn't happen if I run the program in Debug mode (in
Visual Studio, press F5 key to launch debugging). And the exception raises
when I run by Ctrl + F5 (without debugging). Below is my code in the

=== SAMPLE CODE ====

var pathValue = Environment.GetEnvironmentVariable("PATH",
pathValue += @";C:\Program Files (x86)\FWTools2.4.7\bin";
Environment.SetEnvironmentVariable("PATH", pathValue,

using (var ds = Ogr.Open("EXTRACT_POLYGON.tab", 0))
for (int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++)
var layer = ds.GetLayerByIndex(iLayer);
if (layer == null)
throw new ArgumentException("Layer not found");

using (layer)
using (var feature = layer.GetNextFeature())
 using (var geo = feature.GetGeometryRef())
var gmlContent = geo.ExportToGML(); // <=== exception throws, this happens
for ExportToJson(), ExportToKML() ...

===== END ======

And below is the exception

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
       at OSGeo.OGR.OgrPINVOKE.Geometry_ExportToGML(HandleRef jarg1)
       at OSGeo.OGR.Geometry.ExportToGML()
       at TestApp.MainForm.btnRequestToken_Click(Object sender, EventArgs
e) in D:\TestApp\MainForm.cs:line 56

I also get the same exception when retrieving the driver too. For example,
this code will raise the System.AccessViolationException : var driver =

Did I miss something ? Any help is greatly appreciated

Duy Lam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20120111/6b9e6a07/attachment.htm 

More information about the FWTools mailing list