[Gdal-dev] memory leak in MapInfo layer

Richard Matsunaga richard.matsunaga at waypointinfo.com
Tue Jun 13 19:36:17 EDT 2006


Hi Mateusz,

Here is a sample of the code I am using. I have removed non-OGR related
details. For those who may not know, the 'using' statement is used to force
Dispose to be called once out of scope of the using statement. For OGR
objects that have to be cleaned up by the calling code, IDisposable is
implemented to clean up these objects. So in this example, OgrDataSource,
OgrSpatialReference and OgrFeature objects are contained within using
statements to ensure they are disposed of immediately. In addition, I
explicitly destroy the field definition objects that I create.

The C# wrapper is basically the same as the one I sent out some time ago,
although there have been changes - extended functionality, bug fixes and
some unrelated memory issues caused by the wrapper itself. I can send this
out again if that would help. I am fairly confident at this point that the
wrapper is not the cause of the leak, but since I am far from being a
platform invoke expert, there is a, of course, chance that some unexpected
behaviour with the wrapper is the cause.

I am not using any high tech means to detect memory leaks. I run the code
below in a loop and watch the system memory climb and climb. The .NET
garbage collection can be a finicky thing to an outside observer such as
myself, but it will not allow the system to run out of memory unless there
is a leak of some kind, and that can be a problem with managed resources or
unmanaged code. I have run a few .NET memory profilers and they have not
found any problems. There is another commercial product that I have not
tried (yet) that is able to instrument both managed and unmanaged code for
memory profiling.

If I do the same loop test with the Shapefile datasource or by skipping the
'feature.SetGeometryDirectly(point)' step, the memory usage remains static,
as expected.

Wow, this is a long email. Sorry for that :)

Richard

---------------

OgrDriver driver = OgrDriver.GetDriverByName(driverName);

using (OgrDataSource dataSource = driver.CreateDataSource(outputPath,
driverOptions))
{
	Collection<OgrFieldDefinition> fieldDefinitions =
CreateFieldDefinitions();

	using (OgrSpatialReference spatialReference = new
OgrSpatialReference())
	{
		string projectionString = "UTM " + this.utmZone + " (WGS84)
in northern hemisphere.";
	
spatialReference.SetProjectionCoordinateSystem(projectionString);
	
spatialReference.SetWellKnownGeographicCoordinateSystem("WGS84");
		spatialReference.SetUtm(this.utmZone, this.north);

		OgrLayer layer = dataSource.CreateLayer(layerName,
spatialReference,
			OgrWkbGeometryType.wkbPolygon);

		// add fields
		for (int fieldIndex = 0; fieldIndex <
fieldDefinitions.Count; fieldIndex++)
		{
			if (layer.CreateField(fieldDefinitions[fieldIndex],
false) != OgrErr.None)
			{
				throw new OgrException();
			}
		}

		foreach (/*data point to be written to file*/)
		{
			ArrayList fieldValues = GetFieldValues();

			using (OgrFeature feature = new
OgrFeature(layer.LayerFeatureDefinition))
			{
				for (int fieldIndex = 0; fieldIndex <
fieldValues.Count; fieldIndex++)
				{
					feature.SetField(fieldIndex,
fieldValues[fieldIndex]);
				}

				OgrPoint point = new OgrPoint(point.X,
point.Y);

				feature.SetGeometryDirectly(point);

	
feature.SetStyleString(/*MapInfoStyleString*/);
			}

			layer.AddFeature(feature);
		}
	}

	foreach (OgrFieldDefinition fieldDefinition in fieldDefinitions)
	{
		fieldDefinition.Destroy();
	}
} 

-----Original Message-----
From: gdal-dev-bounces at lists.maptools.org
[mailto:gdal-dev-bounces at lists.maptools.org] On Behalf Of Mateusz Loskot
Sent: June 13, 2006 3:56 PM
To: gdal-dev at lists.maptools.org
Subject: Re: [Gdal-dev] memory leak in MapInfo layer

Richard Matsunaga wrote:
> I believe I am experiencing a memory leak in MapInfo tab layers using  
> the C API (called by C# code).


Is this your own C# binding?
Could you provide a piece of code?


> I am writing points to a MapInfo tab file. If I don't add the point 
> object to the OgrFeature, there is no leak. As soon as I add the point 
> (or any geometry), the memory leak occurs.


How are you adding the geometry to feature?
There is a different semanting between SetGeometry and SetGeometryDirectly
regarding ownership of passed geometry.
BTW, how are you detecting memory leaks?

> If I choose a Shapefile datasource, there is no leak.


Hmm, so I'd suspect MapInfo driver is leaking but I'd need to know more
details about how are you using it.


> Could there be something I am doing wrong that is leading to this 
> problem with MapInfo files?


Sure there could be a problem with using OGR incorrectly what can lead to
memory leaks.

> Another test that I did to confirm was to use 'OGR_F_SetGeometry' 
> instead of 'OGR_F_SetGeometryDirectly' (and destroy the original 
> geometry myself) and the same leak occurs.

Good test. So, something other place is leaking.
Please, provide us with more details about:
- C# bindings you are using
- piece of code you are creating/destroying geometries

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
_______________________________________________
Gdal-dev mailing list
Gdal-dev at lists.maptools.org
http://lists.maptools.org/mailman/listinfo/gdal-dev





More information about the Gdal-dev mailing list