Skip to content

Commit 3d6f666

Browse files
nraynaudvisr
authored andcommitted
alter the generator to free strings returned by GEOS.
1 parent 0b1dc52 commit 3d6f666

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

gen/generator.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Clang.LibClang
12
using Clang.Generators
23
using MacroTools
34
using GEOS_jll
@@ -13,7 +14,13 @@ function rewrite(ex::Expr)
1314
cc = :(@ccall $lib.$cname($(cargs...))::$rettype)
1415

1516
cc′ = if rettype == :Cstring
16-
:(unsafe_string($cc))
17+
# do not try to free a const char *
18+
# this is the only function returning such type
19+
if cname == :GEOSversion
20+
:(unsafe_string($cc))
21+
else
22+
:(transform_c_string($cc))
23+
end
1724
else
1825
cc
1926
end

src/LibGEOS.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ export Point,
9494
STRtree,
9595
query
9696

97+
function transform_c_string(s::Cstring)::String
98+
@show "freeing string"
99+
copy = unsafe_string(s)
100+
global _context
101+
GEOSFree_r(_context.ptr, Ptr{Cvoid}(s))
102+
return copy
103+
end
104+
97105
include("libgeos_api.jl")
98106

99107
mutable struct GEOSError <: Exception

src/libgeos_api.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ function GEOSRelatePattern_r(handle, g1, g2, pat)
12891289
end
12901290

12911291
function GEOSRelate_r(handle, g1, g2)
1292-
unsafe_string(
1292+
transform_c_string(
12931293
@ccall(
12941294
libgeos.GEOSRelate_r(
12951295
handle::GEOSContextHandle_t,
@@ -1309,7 +1309,7 @@ function GEOSRelatePatternMatch_r(handle, mat, pat)
13091309
end
13101310

13111311
function GEOSRelateBoundaryNodeRule_r(handle, g1, g2, bnr)
1312-
unsafe_string(
1312+
transform_c_string(
13131313
@ccall(
13141314
libgeos.GEOSRelateBoundaryNodeRule_r(
13151315
handle::GEOSContextHandle_t,
@@ -1330,7 +1330,7 @@ function GEOSisValid_r(handle, g)
13301330
end
13311331

13321332
function GEOSisValidReason_r(handle, g)
1333-
unsafe_string(
1333+
transform_c_string(
13341334
@ccall(
13351335
libgeos.GEOSisValidReason_r(
13361336
handle::GEOSContextHandle_t,
@@ -1408,7 +1408,7 @@ function GEOSRemoveRepeatedPoints_r(handle, g, tolerance)
14081408
end
14091409

14101410
function GEOSGeomType_r(handle, g)
1411-
unsafe_string(
1411+
transform_c_string(
14121412
@ccall(
14131413
libgeos.GEOSGeomType_r(
14141414
handle::GEOSContextHandle_t,
@@ -1827,7 +1827,7 @@ function GEOSWKTWriter_destroy_r(handle, writer)
18271827
end
18281828

18291829
function GEOSWKTWriter_write_r(handle, writer, g)
1830-
unsafe_string(
1830+
transform_c_string(
18311831
@ccall(
18321832
libgeos.GEOSWKTWriter_write_r(
18331833
handle::GEOSContextHandle_t,
@@ -2038,7 +2038,7 @@ function GEOSGeoJSONWriter_destroy_r(handle, writer)
20382038
end
20392039

20402040
function GEOSGeoJSONWriter_writeGeometry_r(handle, writer, g, indent)
2041-
unsafe_string(
2041+
transform_c_string(
20422042
@ccall(
20432043
libgeos.GEOSGeoJSONWriter_writeGeometry_r(
20442044
handle::GEOSContextHandle_t,
@@ -3294,7 +3294,7 @@ function GEOSWKTWriter_destroy(writer)
32943294
end
32953295

32963296
function GEOSWKTWriter_write(writer, g)
3297-
unsafe_string(
3297+
transform_c_string(
32983298
@ccall(
32993299
libgeos.GEOSWKTWriter_write(
33003300
writer::Ptr{GEOSWKTWriter},
@@ -3450,7 +3450,7 @@ function GEOSGeoJSONWriter_destroy(writer)
34503450
end
34513451

34523452
function GEOSGeoJSONWriter_writeGeometry(writer, g, indent)
3453-
unsafe_string(
3453+
transform_c_string(
34543454
@ccall(
34553455
libgeos.GEOSGeoJSONWriter_writeGeometry(
34563456
writer::Ptr{GEOSGeoJSONWriter},
@@ -3511,7 +3511,7 @@ function GEOSGeomFromWKT_r(handle, wkt)
35113511
end
35123512

35133513
function GEOSGeomToWKT_r(handle, g)
3514-
unsafe_string(
3514+
transform_c_string(
35153515
@ccall(
35163516
libgeos.GEOSGeomToWKT_r(
35173517
handle::GEOSContextHandle_t,
@@ -3577,7 +3577,7 @@ function GEOSGeomFromWKT(wkt)
35773577
end
35783578

35793579
function GEOSGeomToWKT(g)
3580-
unsafe_string(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
3580+
transform_c_string(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
35813581
end
35823582

35833583
function GEOS_getWKBOutputDims()

0 commit comments

Comments
 (0)