[geos-devel] Question about geos::io::Unload::Release()

Mateusz Łoskot mateusz at loskot.net
Sat Mar 11 14:14:35 EST 2006


Hi,

Could anyone explain me the idea behind Unload class and its Release 
function?
I'm also interested in the idea behind this static member:
const GeometryFactory* Geometry::INTERNAL_GEOMETRY_FACTORY;

What semantic exactly those entities represent?
What is the lifetime of Geometry::INTERNAL_GEOMETRY_FACTORY;

IMHO geos::io::Unload::Release() may confuse users and they can use it 
in wrong way pretty easily.

Let's consider small example:

Here is a file test.cpp and in that file I have two
functions: foo and bar.
Inside each function I have some automatic objects: local 
GeometryFactory and PrecisionModel and I also create new geometries.
Nothing comes in and goes out to/from those functions.

// File: test.cpp

#include <geos/unload.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Point.h>
#include <geos/geom/PrecisionModel.h>

void run()
{
    foo();
    bar(); <--- BANG!
}

void foo()
{
     geos::geom::PrecisionModel pm(1);
     geos::geom::GeometryFactory gf(&pm, 0);

     geos::geom::Coordinate c1(5, 6);
     geos::geom::Point* p1 = gf.createPoint(c1);
     geos::geom::Geometry* e1 = p1->getEnvelope();

     // Do something else
     // ...

     // MEMORY CLEAN UP
     delete e1;
     delete p1;
     geos::io::Unload::Release(); // <----------- HERE IS OK
}

void bar()
{
     geos::geom::PrecisionModel pm(1);
     geos::geom::GeometryFactory gf(&pm, 0);

     geos::geom::Coordinate c1(5, 6);
     geos::geom::Point* p1 = gf.createPoint(c1);
     geos::geom::Geometry* e1 = p1->getEnvelope();

     // Do something else
     // ...

     // MEMORY CLEAN UP
     delete e1;
     delete p1;
     geos::io::Unload::Release(); // <----------- BANG! BANG!
}

Simply, it seems Unload::Release() can not be called twice in the same 
module/program. Is this correct assumption and behaviour?

It's not clear for my why to not to hide clean up operations inside 
destructors or why not to make some of "long living single objects" as 
singletons that are safe regarding memory management?
If relations are more complex and the number of global objects that we 
need to release is not known, then I'd suggest to provide a kind of 
arena idiom or smart pointer to get garbage collected properly.

Please, review current situation.

Cheers
-- 
Mateusz Łoskot
http://mateusz.loskot.net



More information about the geos-devel mailing list