Opened 11 years ago

Closed 11 years ago

#5045 closed defect (invalid)

A crash about using gdal dll when create a Polygon using new Polygon

Reported by: cpponly2008 Owned by: warmerdam
Priority: normal Milestone:
Component: default Version: unspecified
Severity: normal Keywords:
Cc:

Description (last modified by cpponly2008)

I try to create a polygon geometry by using gdal module int the following code in my workspace, but it is crashed when I debug the program, and it runs well in non-debug status.

void testCreatePolygon()
{
	//this function is crashed when debugging in my own project
	OGRPolygon *oPolygon1 = new OGRPolygon;
	OGRPoint op1(0.0, 0.0);
	OGRPoint op2(0.0, 1.0);
	OGRPoint op3(1.0, 1.0);
	OGRPoint op4(1.0, 0.0);
	OGRPoint op5(0.0, 0.0);
	OGRLinearRing oLR1;
	oLR1.addPoint(&op1);
	oLR1.addPoint(&op2);
	oLR1.addPoint(&op3);
	oLR1.addPoint(&op4);
	oLR1.addPoint(&op5);
	oPolygon1->addRing(&oLR1);//also crashed
	delete oPolygon1;
}

The error point to the following gdal source code:

void OGRPolygon::empty()

{
    if( papoRings != NULL )
    {
        for( int i = 0; i < nRingCount; i++ )
        {
            delete papoRings[i];//this crash
        }
        OGRFree( papoRings );
    }

    papoRings = NULL;
    nRingCount = 0;
}

Then I look up the source code in the gdal module, there is the same code as I do. I debug the code, it isn't crashed. So I consider there is something wrong when cross-gdal dll calling.

Change History (4)

comment:1 by Even Rouault, 11 years ago

Resolution: invalid
Status: newclosed

Using the C++ API isn't recommanded from the exterior of GDAL if you don't use the same compiler & compiler options in both your program and GDAL.

The error in your code is to call oPolygon1->addRingDirectly(&oLR1); that will take ownership of the linear ring and try to delete it. You should rather use addRing() that will make a copy (or dynamically allocate the ring with new, but then you can run into cross-heap issues)

in reply to:  1 comment:2 by cpponly2008, 11 years ago

Description: modified (diff)

Replying to rouault:

Using the C++ API isn't recommanded from the exterior of GDAL if you don't use the same compiler & compiler options in both your program and GDAL.

The error in your code is to call oPolygon1->addRingDirectly(&oLR1); that will take ownership of the linear ring and try to delete it. You should rather use addRing() that will make a copy (or dynamically allocate the ring with new, but then you can run into cross-heap issues)

Sorry, I used addRing(), then the crash occurred too.

comment:3 by cpponly2008, 11 years ago

Resolution: invalid
Status: closedreopened

comment:4 by Even Rouault, 11 years ago

Resolution: invalid
Status: reopenedclosed

I've reproduced the issue with a test app that doesn't use the same compilation flags as libgdal, and just does "OGRPolygon* poPoly = new OGRPolygon; delete poPoly;". I thought that using new/delete would work, but apparently not (this might be due to the virtual destructor of OGRGeometry* that will be used in the libgdal context). Windows cross-heap hell !

The safe way is to use the OGR allocators/destructors :

OGRPolygon *poPoly = (OGRPolygon*) OGRGeometryFactory::createGeometry(wkbPolygon); 
OGRGeometryFactory::destroyGeometry(poPoly );

Still thinking it is not GDAL related, but more an issue with Windows developement with C++

Note: See TracTickets for help on using tickets.