[pgrouting-dev] Using pfree/palloc in c++ code.

Stephen Woodbridge woodbri at swoodbridge.com
Wed Aug 14 17:21:32 PDT 2013


On 8/14/2013 6:19 PM, Dave Potts wrote:
> I am trying to write some code in c++
>
> Its suggested that I should pfree/pmalloc to delete and free memory.
>
> Problem is that when I use them,  I get an error from the loader
>
> select pgr_sql_check_tsp('select * from edge4');
> ERROR:  could not load library
> "/usr/lib/postgresql/9.1/lib/librouting_tsp.so":
> /usr/lib/postgresql/9.1/lib/librouting_tsp.so: undefined symbol:
> _Z18MemoryContextAllocP17MemoryContextDatam
> atsp=#
>
> It looks like a name mangling issue
>
> I tried inserting extern "C" statments around the postgres include files
> (palloc.h) in my code because I discovered that, postgres source does
> not do this, results same issue.
>
> I noticed that ./src/apsp_warshall/src/apsp_boost_wrapper.cpp, has an
> instance of pfree commented out, is there a reasons for this?
>
> Does anybody known
>
> 1.  Is it possible to use pfree/palloc in a potgres cpp file?

I don't recommend doing this because this memory is allocated in various 
contexts that are not always obvious and if the context goes out of 
scope the all the memory allocated in that context is no longer valid. 
Also memory that is palloc does not need to be pfree'd because it gets 
cleaned when the context is done with.

> 2.  Can I use  new/malloc/delete etc instead of pfree/palloc

Yes this is the way to go, there are no problems with this. One thing I 
will comment on with C++ is that if you are using std::vector, then do 
not push back large struct objects, instead create them with new() and 
push a pointer onto std::vector then later, delete all the objects based 
on their points on the std::vector. We had to change a lot of code to 
prevent std::bad_alloc when we tried to push the objects rather than 
their pointers.

-Steve

> regards
>
> Dave.
> _______________________________________________
> pgrouting-dev mailing list
> pgrouting-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/pgrouting-dev



More information about the pgrouting-dev mailing list