<div dir="ltr"><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">Hello,</span><div style="font-size:13.1999998092651px;line-height:19.7999992370605px"><br></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">I am developing a C++ extension (most of the code is C++) for postgres that links dynamically with liblwgeom, without linking to postgis. I call liblwgeom functions that serialize/deserialize LWGEOM* (and similar structures) that don't need a backend like GEOS.</div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px"><br></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px"><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">I wonder how is the memory freed when we call lwerror, as the transaction will be terminated (elog/ereport create a long jump), so if I call liblwgeoms functions from within C++, the stack will not be unwind and even if I use smart pointers it wouldn't make a difference (right?).</div><div><br></div></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">On the contrary, w<span style="font-size:13.1999998092651px;line-height:19.7999992370605px">hen Postgis module loads itself, in _PG_init it overrides memory allocation functions of liblwgeom with pg_alloc and pg_free. Which in turn call palloc and pfree. And in this case when we call lwerror, the memory that we allocated is freed automatically (right?).</span></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px"><span style="font-size:13.1999998092651px;line-height:19.7999992370605px"><br></span></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">I guess (just a guess) it has something to do with the memory context and when a memory context is "closed" the entire memory allocated within would be freed. But lwalloc by default is malloc, so does Postgres do something extremely clever like overriding malloc with its palloc?</div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px"><br></div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">Thank you,</div><div style="font-size:13.1999998092651px;line-height:19.7999992370605px">Igor</div></div>