[postgis-devel] PgSQL Memory Stuff
Nicklas Avén
nicklas.aven at jordogskog.no
Wed Dec 8 13:13:00 PST 2010
Thanks a lot Paul !!!
This makes things clearer for me :-)
Then I just would like to add freeing the bbox also for the point-case
in lwpoint_free. (or what the name was)
Then we will get rid of "my" memory leak with the left bounding box that
I create.
But what about some earlier discussion/explanation you gave about the
difficulty to know if the pointer to the point-array in the
LWGEOM-representation is pointing directly to the stored version or a
copied version of the point-array?
How is that handled?
Or did I miss understand something?
Getting this things right makes things more fun.
Thanks again
Nicklas
On Tue, 2010-12-07 at 16:27 -0800, Paul Ramsey wrote:
> Nik,
> Here's an annotated version of one of your functions, none of this
> stuff is critical, since pgsql just cleans right up behind you, but in
> case you want to do it "right"...
>
>
> PG_FUNCTION_INFO_V1(LWGEOM_shortestline2d);
> Datum LWGEOM_shortestline2d(PG_FUNCTION_ARGS)
> {
> PG_LWGEOM *result;
> /* Detoast the actual PgSQL varlena structures, in our case PG_LWGEOM
> (soon to be GSERIALIZED) */
> PG_LWGEOM *geom1 = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
> PG_LWGEOM *geom2 = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
> /* Build LWGEOM from the varlena (soon to be with lwgeom_from_gserialized) */
> LWGEOM *lwgeom1 = pglwgeom_deserialize(geom1);
> LWGEOM *lwgeom2 = pglwgeom_deserialize(geom2);
> LWGEOM *theline;
>
>
> if (lwgeom1->srid != lwgeom2->srid)
> {
> elog(ERROR,"Operation on two GEOMETRIES with different SRIDs\n");
> PG_RETURN_NULL();
> }
>
> theline = lw_dist2d_distanceline(lwgeom1, lwgeom2, lwgeom1->srid, DIST_MIN);
> if (lwgeom_is_empty(theline))
> PG_RETURN_NULL();
>
> /* Serialize the result back down from LWGEOM, but don't return right away */
> result = pglwgeom_serialize(theline);
> /* First free the LWGEOMs you used */
> lwgeom_free(lwgeom1);
> lwgeom_free(lwgeom2);
>
> /* Then call free_if_copy on the *varlena* structures you originally
> get as arguments */
> PG_FREE_IF_COPY(geom1, 0);
> PG_FREE_IF_COPY(geom2, 1);
>
> /* And now return */
> PG_RETURN_POINTER(result);
> }
> _______________________________________________
> postgis-devel mailing list
> postgis-devel at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-devel
>
More information about the postgis-devel
mailing list