Использование функций из GEOS C api Ошибка сегментации возврата

Я начал использовать C api вместо C ++ api от GEOS. Но когда я использую некоторые функции, такие как GEOSDistance() или что-то еще, я получаю ошибку сегментации.

Вот фрагменты моего кода, которые терпят неудачу:

double MyClass::dist( const GEOSGeometry * g1, const GEOSGeometry * g2)
{
double dist;
GEOSDistance( g1, g2, &dist );
return distance;
}
...

int main()
{
MyClass mc;
...
double d = mc.dist( g1, g2);   // segmentation fault, g1, g2 are const GEOSGeometry *
...
GEOSGeometry *g = ...  // point, line or polygon geometry I got from other source

const GEOSCoordSequence *s = GEOSGeom_getCoordSeq( g );
GEOSCoordSequence *c = GEOSCoordSeq_clone( s ); // segmentation fault}

Я не уверен, как именно использовать GEOS C API. Должен ли я использовать некоторые initGeos() а также finishGeos() функции? Почему существуют эти ошибки сегментации?

Я взглянул на этот исходный файл — http://trac.osgeo.org/geos/browser/trunk/tests/geostest/geostest.c, но мне кажется, что они используют его так же, как и я. Где моя ошибка?

Редакция:
Вот вывод из valgrind: (с опцией —tool = memcheck)

==19336== Invalid read of size 8
==19336==    at 0x6626FA0: GEOSDistance_r (geos_ts_c.cpp:996)
==19336==    by 0xC8C3835: VertexSnapper::isClose(GEOSGeom_t const*, GEOSGeom_t const*) (vertexsnapper.cpp:118)
==19336==    by 0xC8C34A2: VertexSnapper::snap() (vertexsnapper.cpp:40)
==19336==    by 0x5B395B6: QgsConflateProvider::vertexSnap() (qgsconflateprovider.cpp:288)
==19336==    by 0x401CB0: main (main.cpp:81)
==19336==  Address 0x1c27af30 is 16 bytes inside a block of size 56 free'd
==19336==    at 0x4C2B1BF: operator delete(void*) (vg_replace_malloc.c:480)
==19336==    by 0x6628C61: GEOSGeom_destroy_r (geos_ts_c.cpp:2095)
==19336==    by 0x5104D31: QgsGeometry::~QgsGeometry() (qgsgeometry.cpp:233)
==19336==    by 0x5101D28: QgsFeature::setGeometry(QgsGeometry*) (qgsfeature.cpp:138)
==19336==    by 0x5101DB1: QgsFeature::setGeometryAndOwnership(unsigned char*, unsigned long) (qgsfeature.cpp:152)
==19336==    by 0xC6AFA3E: QgsOgrFeatureIterator::readFeature(void*, QgsFeature&) (qgsogrfeatureiterator.cpp:255)
==19336==    by 0xC6AF298: QgsOgrFeatureIterator::nextFeature(QgsFeature&) (qgsogrfeatureiterator.cpp:140)
==19336==    by 0x50634B9: QgsFeatureIterator::nextFeature(QgsFeature&) (qgsfeatureiterator.h:114)
==19336==    by 0x520C1D6: QgsVectorLayerFeatureIterator::nextFeature(QgsFeature&) (qgsvectorlayerfeatureiterator.cpp:103)
==19336==    by 0x5B3923F: QgsConflateProvider::transferGeometrytoGeos(bool) (qgsconflateprovider.cpp:191)
==19336==    by 0x5B3953A: QgsConflateProvider::vertexSnap() (qgsconflateprovider.cpp:272)
==19336==    by 0x401CB0: main (main.cpp:81)
==19336==
==19336== Invalid read of size 8
==19336==    at 0xCBDB60A: geos::operation::distance::DistanceOp::distance() (DistanceOp.cpp:147)
==19336==    by 0xCBDB7CD: geos::operation::distance::DistanceOp::distance(geos::geom::Geometry const*, geos::geom::Geometry const*) (DistanceOp.cpp:63)
==19336==    by 0xCB76B58: geos::geom::Geometry::distance(geos::geom::Geometry const*) const (Geometry.cpp:766)
==19336==    by 0x6626FAB: GEOSDistance_r (geos_ts_c.cpp:996)
==19336==    by 0xC8C3835: VertexSnapper::isClose(GEOSGeom_t const*, GEOSGeom_t const*) (vertexsnapper.cpp:118)
==19336==    by 0xC8C34A2: VertexSnapper::snap() (vertexsnapper.cpp:40)
==19336==    by 0x5B395B6: QgsConflateProvider::vertexSnap() (qgsconflateprovider.cpp:288)
==19336==    by 0x401CB0: main (main.cpp:81)
==19336==  Address 0x1c27af30 is 16 bytes inside a block of size 56 free'd
==19336==    at 0x4C2B1BF: operator delete(void*) (vg_replace_malloc.c:480)
==19336==    by 0x6628C61: GEOSGeom_destroy_r (geos_ts_c.cpp:2095)
==19336==    by 0x5104D31: QgsGeometry::~QgsGeometry() (qgsgeometry.cpp:233)
==19336==    by 0x5101D28: QgsFeature::setGeometry(QgsGeometry*) (qgsfeature.cpp:138)
==19336==    by 0x5101DB1: QgsFeature::setGeometryAndOwnership(unsigned char*, unsigned long) (qgsfeature.cpp:152)
==19336==    by 0xC6AFA3E: QgsOgrFeatureIterator::readFeature(void*, QgsFeature&) (qgsogrfeatureiterator.cpp:255)
==19336==    by 0xC6AF298: QgsOgrFeatureIterator::nextFeature(QgsFeature&) (qgsogrfeatureiterator.cpp:140)
==19336==    by 0x50634B9: QgsFeatureIterator::nextFeature(QgsFeature&) (qgsfeatureiterator.h:114)
==19336==    by 0x520C1D6: QgsVectorLayerFeatureIterator::nextFeature(QgsFeature&) (qgsvectorlayerfeatureiterator.cpp:103)
==19336==    by 0x5B3923F: QgsConflateProvider::transferGeometrytoGeos(bool)     (qgsconflateprovider.cpp:191)
==19336==    by 0x5B3953A: QgsConflateProvider::vertexSnap() (qgsconflateprovider.cpp:272)
==19336==    by 0x401CB0: main (main.cpp:81)
==19336==
pure virtual method called
terminate called without an active exception
==19336==
==19336== HEAP SUMMARY:
==19336==     in use at exit: 410,183 bytes in 3,786 blocks
==19336==   total heap usage: 54,587 allocs, 50,801 frees, 23,308,451 bytes allocated
==19336==
==19336== LEAK SUMMARY:
==19336==    definitely lost: 48 bytes in 1 blocks
==19336==    indirectly lost: 3 bytes in 1 blocks
==19336==      possibly lost: 1,845 bytes in 50 blocks
==19336==    still reachable: 408,287 bytes in 3,734 blocks
==19336==         suppressed: 0 bytes in 0 blocks
==19336== Rerun with --leak-check=full to see details of leaked memory
==19336==
==19336== For counts of detected and suppressed errors, rerun with: -v
==19336== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 5 from 1)
Neúspěšně ukončen (SIGABRT) (core dumped [obraz paměti uložen])

Кажется, что проблема не с GEOS. Я прав?

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …