[geos-devel] using CoordinateOperation::edit cause memory leaks

Tereza Fiedlerová tfiedlerova at gmail.com
Wed May 22 00:58:20 PDT 2013


Hi,

I'm sorry for my long reaction time, but I have still the same problem
and I hope you still can help me. I tried to create a minimal example.
This example returns both my problems - memory leak because of using
WKT and also the problem with GeometryEditorOperation as I wrote
before.
The input is simple wkt polygon and I just trying to change its
position. The main.cpp file is attached to this email.

The output from valgrind --leak-check=full is as follows:

==3366== Memcheck, a memory error detector
==3366== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3366== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3366== Command: ./Example
==3366==
==3366==
==3366== HEAP SUMMARY:
==3366==     in use at exit: 1,112 bytes in 22 blocks
==3366==   total heap usage: 82 allocs, 60 frees, 9,200 bytes allocated
==3366==
==3366== 520 (64 direct, 456 indirect) bytes in 1 blocks are
definitely lost in loss record 21 of 22
==3366==    at 0x4C2C154: operator new(unsigned long) (vg_replace_malloc.c:298)
==3366==    by 0x4ED5CEA:
geos::geom::GeometryFactory::createPolygon(geos::geom::LinearRing*,
std::vector<geos::geom::Geometry*,
std::allocator<geos::geom::Geometry*> >*) const (in
/usr/lib/libgeos-3.3.3.so)
==3366==    by 0x4EE1718:
geos::geom::util::GeometryEditor::editPolygon(geos::geom::Polygon
const*, geos::geom::util::GeometryEditorOperation*) (in
/usr/lib/libgeos-3.3.3.so)
==3366==    by 0x4EE1423:
geos::geom::util::GeometryEditor::edit(geos::geom::Geometry const*,
geos::geom::util::GeometryEditorOperation*) (in
/usr/lib/libgeos-3.3.3.so)
==3366==    by 0x400EE1: main (in
/host/Users/Tereza/Documents/CVUT-FSv/BP/Example/Example)
==3366==
==3366== 592 (64 direct, 528 indirect) bytes in 1 blocks are
definitely lost in loss record 22 of 22
==3366==    at 0x4C2C154: operator new(unsigned long) (vg_replace_malloc.c:298)
==3366==    by 0x4ED5CEA:
geos::geom::GeometryFactory::createPolygon(geos::geom::LinearRing*,
std::vector<geos::geom::Geometry*,
std::allocator<geos::geom::Geometry*> >*) const (in
/usr/lib/libgeos-3.3.3.so)
==3366==    by 0x4F09E3E:
geos::io::WKTReader::readPolygonText(geos::io::StringTokenizer*) (in
/usr/lib/libgeos-3.3.3.so)
==3366==    by 0x4F0B053:
geos::io::WKTReader::readGeometryTaggedText(geos::io::StringTokenizer*)
(in /usr/lib/libgeos-3.3.3.so)
==3366==    by 0x4F0B49B: geos::io::WKTReader::read(std::string
const&) (in /usr/lib/libgeos-3.3.3.so)
==3366==    by 0x400EAD: main (in
/host/Users/Tereza/Documents/CVUT-FSv/BP/Example/Example)
==3366==
==3366== LEAK SUMMARY:
==3366==    definitely lost: 128 bytes in 2 blocks
==3366==    indirectly lost: 984 bytes in 20 blocks
==3366==      possibly lost: 0 bytes in 0 blocks
==3366==    still reachable: 0 bytes in 0 blocks
==3366==         suppressed: 0 bytes in 0 blocks
==3366==
==3366== For counts of detected and suppressed errors, rerun with: -v
==3366== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)


I will really appreciate your help. Thanks

Tereza






2013/5/16 Sandro Santilli <strk at keybit.net>:
> What I need is a full contained test.
> A .cpp file that built and run exposes the leak.
> It must include the geometry input.
>
> --strk;
>
> On Wed, May 15, 2013 at 10:42:08PM +0200, Tereza Fiedlerová wrote:
>> 2013/5/13 Sandro Santilli <strk at keybit.net>:
>> >
>> > Hi Tereza, could you include a full testcase, including caller ?
>> >
>> > --strk;
>>
>> Hi,
>>
>> I tried to make it easier, but I see it wasn't good idea, so I'm
>> giving here the necessary code without changes:
>>
>> vertexgeometryeditoroperation.h
>> --------------------------------
>>
>> #ifndef VERTEXGEOMETRYEDITOROPERATION_H
>> #define VERTEXGEOMETRYEDITOROPERATION_H
>>
>> // GEOS includes
>> #include <geos/geom/CoordinateSequence.h>
>> #include <geos/geom/Geometry.h>
>> #include <geos/geom/util/CoordinateOperation.h>
>> #include <geos/geom/CoordinateSequenceFactory.h>
>>
>> // local includes
>> #include "geoc.h"
>>
>> #include <QtGui>
>>
>> using namespace std;
>> using namespace geos;
>> using namespace geos::geom;
>> using namespace geos::geom::util;
>>
>> using namespace geoc;
>> using namespace geoc::geo;
>> using namespace geoc::alg;
>> using namespace geoc::edit;
>>
>> namespace geoc {
>> namespace edit {
>>
>> /** Class for editing geometry - snap vertices to the closest points. */
>>
>> class VertexGeometryEditorOperation: public CoordinateOperation
>> {
>>
>>     using CoordinateOperation::edit;
>>
>> public:
>>
>>     /// Constructor
>>     VertexGeometryEditorOperation(  ): closeCoord(NULL),
>> tolDistance(0), changed(false){}
>>
>>     /** Set private closeCoord to given coord.
>>      */
>>     void setCloseCoordSeq( CoordinateSequence *coord ) { closeCoord = coord; }
>>
>>     /** Set tolerance distance
>>       */
>>     void setTolDistance( double tol ) { tolDistance = tol; }
>>
>>     /** Return true if geometry was changed
>>       */
>>     bool isChanged() { return changed; }
>>
>>     /** Virtual function for editing geometry - snap to closest vertices.
>>      * @param coordinates Not important.
>>      * @param gGeometry to be edited.
>>      */
>>     CoordinateSequence* edit(const CoordinateSequence *coordinates,
>> const Geometry *g );
>>
>>
>> private:
>>
>>     CoordinateSequence *closeCoord;
>>     double tolDistance;
>>     bool changed;
>>
>> };
>>
>> } //namespace geoc
>> } //namespace edit
>>
>> #endif // VERTEXGEOMETRYEDITOROPERATION_H
>>
>>
>> ------------------------------
>> vertexgeometryoperation.cpp
>> ------------------------------
>>
>> #include "vertexgeometryeditoroperation.h"
>>
>> namespace geoc {
>> namespace edit {
>>
>> CoordinateSequence* VertexGeometryEditorOperation::edit(const
>> CoordinateSequence *, const Geometry *geom )
>> {
>>     CoordinateSequence* coord = geom->getCoordinates();
>>
>>     // find closest point from closeCoord
>>     for ( size_t i = 0; i < coord->getSize(); i++)
>>     {
>>         // minimal distance
>>         double minDist = tolDistance;
>>         size_t indMin = 0;
>>         bool isMin = false;
>>
>>         for ( size_t j = 0; j < closeCoord->getSize(); j++ )
>>         {
>>             // compute distance between two tested points
>>             double dist = coord->getAt(i).distance( closeCoord->getAt(j) );
>>
>>             if( (0 < dist) && (dist < minDist) )
>>             {
>>                 minDist = dist;
>>                 indMin = j;
>>                 isMin = true;
>>             }
>>
>>         }
>>
>>         // set new coordinate to the closest point if there is some
>>         if ( isMin )
>>         {
>>             coord->setAt( closeCoord->getAt(indMin), i );
>>             changed = true;
>>         }
>>
>>     }
>>
>>     return coord;
>>
>> }
>>
>> } //namespace geoc
>> } //namespace edit
>>
>>
>> --------------------------
>> function using code above
>> --------------------------
>>
>> void VertexSnapper::snapVertices(Geometry *geom, CoordinateSequence *closeCoord)
>> {
>>
>>     // create and set geometry editor
>>     VertexGeometryEditorOperation myOp;
>>     myOp.setCloseCoordSeq( closeCoord );
>>     myOp.setTolDistance( tolDistance );
>>
>>     GeometryEditor geomEdit( geom->getFactory() );
>>
>>     // set geometry to edited one
>>     geom=( geomEdit.edit( geom , &myOp ) );
>>
>>
>> }
>>
>>
>> Thanks for help in advance,
> _______________________________________________
> geos-devel mailing list
> geos-devel at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/geos-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 1194 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/geos-devel/attachments/20130522/ae9df2a2/attachment.cpp>


More information about the geos-devel mailing list