[Gdal-dev] GDAL C# bindings exception: Attempted to read or write
protected memory
Ben Greene
bgreene at rapidmap.com.au
Sun Apr 22 19:51:01 EDT 2007
Hi,
I had a problem with the GDAL C# bindings freezing a little while ago, and Tamas managed to find the problem (I was passing in a two-element array instead of a three element for the return value). However, now I'm having a different issue.
I'm working with 1.4.1 binaries compiled myself according to the instructions on this site (I can't find the most recent binaries in the FWTools distribution). When I run the code below, I get a System.AccessViolationException:Attempted to read or write protected memory exception on the line:
transform.TransformPoint(point, px, py, 0.0)
It appears that the exception is thrown from this line:
public void TransformPoint(double[] argout, double x, double y, double z) {
osrPINVOKE.CoordinateTransformation_TransformPoint__SWIG_1(swigCPtr, argout, x, y, z);
}
The test image I'm using is:
o41078a5.tif
from
http://dl.maptools.org/dl/geotiff/samples/usgs/
Here is the VB.Net code I'm using (in VS 2005):
Imports GDAL
Imports OGR
Public Class GeoTiffReader
Private Shared image As Dataset
Public Shared Function GetGeoReference(ByVal Filename As String) As GeoReference
If Not (Filename Is Nothing) Then
'Return Nothing
'if uncommented, the code will not crash
End If
GDAL.gdal.AllRegister()
image = GDAL.gdal.Open(Filename, 0)
If (image Is Nothing) Then
Return Nothing
End If
Dim gr(6) As Double
image.GetGeoTransform(gr)
Dim topLeft() As Double = TransformWGS84(gr(0), gr(3))
Dim bottomRight() As Double = TransformWGS84(gr(0) + gr(1) * image.RasterXSize, gr(3) + gr(5) * image.RasterXSize)
If (topLeft.Length = 2 And bottomRight.Length = 2) Then
Return New GeoReference(topLeft(0), topLeft(1), bottomRight(0), bottomRight(1))
Else
Return Nothing
End If
image.Dispose()
End Function
Public Shared Function TransformWGS84(ByVal px As Double, ByVal py As Double) As Double()
Dim point() As Double
point = New Double() {0.0, 0.0, 0.0}
Dim srWGS84 As New SpatialReference("")
srWGS84.SetWellKnownGeogCS("WGS84")
If (image.GetProjection() = "") Then
Return New Double() {}
End If
Dim srCurrent As New SpatialReference(image.GetProjection())
Dim transform As New CoordinateTransformation(srCurrent, srWGS84)
If (transform Is Nothing) Then
Return New Double() {}
End If
Try
transform.TransformPoint(point, px, py, 0.0)
Catch
Return New Double() {}
End Try
Return point
End Function
End Class
More information about the Gdal-dev
mailing list