[Gdal-dev] memory leak in MapInfo layer

Mateusz Loskot mateusz at loskot.net
Wed Jun 14 08:33:29 EDT 2006


Richard Matsunaga wrote:
> 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.


Yes, but OgrSpatialReference class is not disposable, so how did you
manage to put it in using?

VC# 2005 throws me this message:
error CS1674: 'GDALWrapper.OGR.OgrSpatialReference': type used in a
using statement must be implicitly convertible to 'System.IDisposable'

Or I have old version (from the beginning of May 2006) of your GDALWrapper?


> 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.


The best way to check is to implement the same functionality in C++
correctly and see if this also will leak.
It should be not very hard to get ESRI Shapefile/MapInfo File driver,
create datasource,
create one layer.


> 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.


I checked the creation of data source  with .NET Memory
Profiler and also watching the memory usage in the tasks list.
I don't see huge increase of memory consuming.
I mean, 1000 layers (1000 x data source, driver, layer objects) gave me
about 2 MB memory usage.
Also the .NET Profiler reports the same number of objects created and
disposed of all classes I used.

I got problems with CreateLayer for Shapefile.
Calling OGR OGR_DS_CreateLayer gave me memory corruption.
I tried various parameters, null checking, etc.
I have no idea what's going wrong there.

But I've shown you, Valgrind on Linux does not show any memory issues
with ogr2ogr - a C++ program using OGR.
Unfortunately, I don't have any other bounds checker on Windows than
debuggers coming with VC++ and VC# 2005.

May be you could do some tests as simple as possible:
- create and add single field
- create and add single point (geometry) to feature
etc.

This way you should be able to find where is the problem in C# wrapper,
if any.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net




More information about the Gdal-dev mailing list