[Gdal-dev] Crash in VC++ in GDALClose(VRTDataset*)

Frank Warmerdam warmerdam at pobox.com
Tue Mar 25 15:38:02 EST 2003

Garvan Keeley wrote:
> Hi,
> I am trying to use some of the code from gdal_translate, but I found it was
> crashing when built under Visual Studio .Net (unmanaged C++).
> Here is a small C++ console app that crashes in the function GDALClose().
> Any idea what might be causing this?
> // assume library paths and linker options are set correctly
> // i.e. add the gdal source tree to additional include dirs and
> // gdal_i.lib added to linker dependencies
> #include "gdal.h"
> #include "frmts\vrt\vrtdataset.h"
> int _tmain(int argc, _TCHAR* argv[])
> {
> 	VRTDataset* poVDS = new VRTDataset( 100, 100 );
> 	GDALClose( (GDALDatasetH) poVDS );


I have tried this on Linux under valgrind and it is fine.  I presume the
problem is that you are allocating the VRTDataset in your main, which will
use one "heap".  Then you free it in GDALClose() which uses the heap assigned
to the GDAL11.DLL.

I would suggest instead that you change "GDALClose((GDALDatasetH) poVDS );"
to "delete poVDS;".

Note, there are ways to build GDAL and your main application so they both
use the C runtime library for allocations and deallocations (effectively
operating against the same heap) but I don't know the details of how that
is accomplished.  Perhaps others could comment.

I have had lots of problems in the past with mixing new/delete calls
between mainlines and the GDAL library.  A more common case is where an
object is allocated in the library and then destroyed with "delete" in
the mainline.

Best regards,

I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the Gdal-dev mailing list