[geos-devel] Memory problems while parsing WKB

Andreas Volz lists at brachttal.net
Mon Apr 9 10:11:37 EDT 2007


Hello,

I've problems to parse WKB with GEOS. Here is my sample application:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <pqxx/pqxx>
#include <proj_api.h>
#include <geos.h>

using namespace std;
using namespace pqxx;
using namespace geos;

projPJ pj;

int main (int argc, char **argv)
{
  result R;

  try
  {
    connection Conn ("dbname=osm");

    work transaction (Conn, "OSMTransaction");

    R = transaction.exec("SELECT name,AsBinary(way) FROM
	planet_osm_point WHERE place = 'city' and name != ''"); 
  } catch (pqxx::broken_connection) 
  {
    cerr << "No connection to database" << endl;
  }

  if (!(pj = pj_init_plus ("+datum=WGS84 +to +proj=utm +zone=32
	+datum=WGS84"))) exit(1);

  for (result::const_iterator i = R.begin(); i != R.end(); ++i)
  {
  	cout << "#City: " << (*i)[0] << endl;

   
    pqxx::binarystring b ((*i)[1]);

    stringstream binaryStream
	(ios_base::binary|ios_base::in|ios_base::out);

    binaryStream << b.str();

    GeometryFactory geoFactory;

    WKBReader wkbReader (geoFactory);

    Geometry *geom;

    try
    {
      geom = wkbReader.read (binaryStream);
    }
    catch (geos::ParseException*)
    {
      cerr << "Parse exception" << endl;
      exit (1);
    }

    cout << geom->getGeometryType() << endl;

    cout << "getNumPoints(): " << geom->getNumPoints () << endl;

    const Coordinate *coord = geom->getCoordinate ();

    cout << "x/y: " << coord->x << "/" << coord->y << endl;
  }

  return 0;
}

It works in most cases, but sometimes the result values are broken or I
get a segfault. So I used valgrind to get some more information. I got
this as result:

==17755== Invalid read of size 4
==17755==    at 0x41327FE: geos::PrecisionModel::makePrecise(double)
const (in /usr/lib/libgeos.so.2.2.3) ==17755==    by 0x401D5D5A: ???
==17755==  Address 0x460A544 is 4 bytes inside a block of size 16 free'd
==17755==    at 0x40212FA: operator delete(void*)
(in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so) ==17755==    by
0x4132695: geos::PrecisionModel::~PrecisionModel()
(in /usr/lib/libgeos.so.2.2.3) ==17755==    by 0x42F083B: (below main)
(in /lib/libc-2.5.so) ==17755==

... and this as often as I read coordinates.

Any ideas why PrecisionModel has this problem? A problem in GEOS WKBParser or my 
application?

regards
Andreas



More information about the geos-devel mailing list