[geos-devel] GEOS, Shapely, DLLs, and memory

Aron Bierbaum aronbierbaum at gmail.com
Mon Apr 27 14:47:19 EDT 2009


We have a rather large application that queries a PostgreSQL database
from multiple threads and creates Shapely/GEOS geometry objects from
multiple threads using the WKB builder. Over the past few weeks we
have been trying to track down a odd crash in our application that
seems to happen when we create a Shapely/GEOS object from WKT in order
to start the DB query. After many hours of debugging we believe we
have a solution. From what we can tell our application was crashing
because memory that was being allocated in the GEOS dll was being
released inside of our application. When converting a GEOS geometry
object to a string, Shapely calls GEOSGeomToWKT_r which creates a char
buffer using std::malloc. After the call Shapely releases the memory
using the standard free function. According to a "Potential Errors
Passing CRT Objects Across DLL Boundaries" [1]:

   "A related problem can occur when you allocate memory (either
explicitly with new or malloc, or implicitly with strdup,
strstreambuf::str, and so on) and then pass a pointer across a DLL
boundary to be freed. This can cause a memory access violation or heap
corruption if the DLL and its users use different copies of the CRT
libraries.

The basic problem is that two DLLs can use different copies of the
CRT. This can lead to many problems if you don't allocated and free
memory in the same DLL. In order to get around this problem we added a
new function to the GEOS capi, GEOSFree, that simply frees memory
allocated in the GEOS DLL. Then in Shapely we just use this function
where we need to ensure that memory allocated and returned from GEOS
is also freed in the GEOS DLL.

I have attached a patch for both GEOS and Shapely that we have been
testing with for a few weeks without any problems. Does anyone have a
better way to address this issue?

Thanks,
Aron Bierbaum
-------------- next part --------------
A non-text attachment was scrubbed...
Name: geos.patch
Type: text/x-patch
Size: 923 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/geos-devel/attachments/20090427/2bbecbd5/geos.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shapely.patch
Type: text/x-patch
Size: 1161 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/geos-devel/attachments/20090427/2bbecbd5/shapely.bin


More information about the geos-devel mailing list