c++gdal

GDAL code crashes, because OGRFieldDefn modifies pointers


I am trying to create and write into a Shapefile.

I am on Windows 11 and have currently installed mingw-w64-ucrt-x86_64-gdal-3.9.3-4 from https://repo.msys2.org/mingw/ucrt64/ (I tried different versions but did not succeed to run them).

When debugging my code, I noticed that the call of OGRFieldDefn modifies the addresses the pointers are pointing to. Later on my code crashes as soon as these erroneous pointers are used.

I was able to reproduce this behaviour in a minimal example:

#include "ogrsf_frmts.h"

int main()
{    
    GDALAllRegister();
    GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName("ESRI Shapefile");

    GDALDataset *poDS;
    poDS = poDriver->Create( "point_out.shp", 0, 0, 0, GDT_Unknown, NULL );

    OGRLayer *poLayer;
    poLayer = poDS->CreateLayer( "point_out", NULL, wkbPoint, NULL );

    cout << "Pointers before call: " << poDS << " " << poLayer << endl;
    OGRFieldDefn oField( "Name", OFTString );
    oField.SetWidth(32);
    cout << "Pointers after call: " << poDS << " " << poLayer << endl;

    GDALClose( poDS );
}

My output of the minimal example is:

Pointers before call: 0x2b3fc77b810 0x2b3fc7720b0
Pointers after call: 0 0xd7603ffd18

So what am I doing wrong? Is it a bug in GDAL? Could you recommend a working GDAL version or setup?


Solution

  • It was a bug. I solved it by updating my CLion IDE, using the newest compiler versions and installing mingw-w64-ucrt-x86_64-gdal-3.10.2-2.