s2
s2 copied to clipboard
memory error
See https://github.com/r-spatial/sf/issues/2213 - the following script causes a memory error
library(sf)
vec = structure(list(geom = structure(list(structure(list(list(structure(c(39.0990310770001,
39.0973986580001, 39.0900920320001, 39.0882280280001, 39.08911197,
39.0894829740001, 39.0918795890001, 39.0967767550001, 39.0980982660001,
39.0990310770001, 46.7626812080001, 46.7577288260001, 46.7584910210001,
46.7586139810001, 46.7634339800001, 46.763648, 46.7666747210001,
46.765822804, 46.764544872, 46.7626812080001), dim = c(10L, 2L
)))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list(
list(structure(c(39.0882278059561, 39.0875431204148, 39.0819033797609,
39.0755368960086, 39.0754178456251, 39.0753169250029, 39.0753915236716,
39.0754203468949, 39.075495678328, 39.0755694321909, 39.0757367656267,
39.0759128007385, 39.0766084650748, 39.0782343662515, 39.0787924205944,
39.0794658153143, 39.0803355102279, 39.0812719416263, 39.0827663638211,
39.0833898069564, 39.0839476903012, 39.0849000937751, 39.0852131733517,
39.0856556550361, 39.0860658102036, 39.0866250300331, 39.0875777040198,
39.0880036927405, 39.0882670850506, 39.0884805072476, 39.0887769628217,
39.0891124501921, 39.0882278059561, 46.7586144147398, 46.7585199896372,
46.7576872449644, 46.7567197161128, 46.7567377929245, 46.7568458491676,
46.7575207397425, 46.758528630568, 46.7591405254797, 46.7595004443845,
46.760058282487, 46.760400135995, 46.7606786458641, 46.7612804902736,
46.7614690903009, 46.7616126109683, 46.7616659986377, 46.7617373297074,
46.7619432398511, 46.762059775212, 46.7621223613409, 46.7621936510026,
46.7622024174623, 46.7622650811303, 46.7623277674016, 46.7624533343503,
46.7626865869324, 46.7629202386809, 46.7630730212041, 46.7631988452449,
46.7632976070905, 46.7634337444997, 46.7586144147398), dim = c(33L,
2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
input = "WGS 84", wkt = "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"), class = "crs"), class = c("sfc_MULTIPOLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 39.0753169250029,
ymin = 46.7567197161128, xmax = 39.0990310770001, ymax = 46.7666747210001
), class = "bbox"))), row.names = 1:2, class = c("sf", "data.frame"
), sf_column = "geom", agr = structure(integer(0), class = "factor", levels = c("constant",
"aggregate", "identity"), names = character(0)), .sf_namespace = function ()
NULL)
st_make_valid(st_as_sfc(vec), s2_options = s2::s2_options(snap = s2::s2_snap_precision(1e+09)))
st_make_valid(st_as_sfc(vec), s2_options = s2::s2_options(snap = s2::s2_snap_precision(1e+18)))
With the following valgrind stacktrace:
> st_make_valid(st_as_sfc(vec), s2_options = s2::s2_options(snap = s2::s2_snap_precision(1e+18)))
==64930== Invalid read of size 4
==64930== at 0x1C7DDE60: S2Builder::AddSnappedEdges(int, int, S2Builder::GraphOptions const&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >*, std::vector<int, std::allocator<int> >*, IdSetLexicon*, std::vector<gtl::compact_array<int, std::allocator<int> >, std::allocator<gtl::compact_array<int, std::allocator<int> > > >*) const (s2builder.cc:1169)
==64930== by 0x1C7E33AD: S2Builder::BuildLayerEdges(std::vector<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >, std::allocator<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > > >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >*, IdSetLexicon*) (s2builder.cc:1134)
==64930== by 0x1C7E36BD: S2Builder::BuildLayers() (s2builder.cc:1057)
==64930== by 0x1C7E52A6: S2Builder::Build(S2Error*) (s2builder.cc:475)
==64930== by 0x1C7AA636: s2geography::s2_rebuild(s2geography::Geography const&, s2geography::GlobalOptions const&, s2geography::GlobalOptions::OutputAction, s2geography::GlobalOptions::OutputAction, s2geography::GlobalOptions::OutputAction) (build.cc:271)
==64930== by 0x1C7AA8FC: s2geography::s2_rebuild(s2geography::Geography const&, s2geography::GlobalOptions const&) (build.cc:286)
==64930== by 0x1C77CED7: cpp_s2_rebuild(Rcpp::Vector<19, Rcpp::PreserveStorage>, Rcpp::Vector<19, Rcpp::PreserveStorage>)::Op::processFeature(Rcpp::XPtr<RGeography, Rcpp::PreserveStorage, &(void Rcpp::standard_delete_finalizer<RGeography>(RGeography*)), false>, long) (s2-transformers.cpp:271)
==64930== by 0x1C764A84: UnaryGeographyOperator<Rcpp::Vector<19, Rcpp::PreserveStorage>, SEXPREC*>::processVector(Rcpp::Vector<19, Rcpp::PreserveStorage>) (geography-operator.h:35)
==64930== by 0x1C77D798: cpp_s2_rebuild(Rcpp::Vector<19, Rcpp::PreserveStorage>, Rcpp::Vector<19, Rcpp::PreserveStorage>) (s2-transformers.cpp:281)
==64930== by 0x1C782BB3: _s2_cpp_s2_rebuild (RcppExports.cpp:1281)
==64930== by 0x49562B9: ??? (in /usr/lib/R/lib/libR.so)
==64930== by 0x4999262: ??? (in /usr/lib/R/lib/libR.so)
==64930== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==64930==
*** caught segfault ***
address (nil), cause 'memory not mapped'
Traceback:
1: cpp_s2_rebuild(as_s2_geography(x), options)
2: structure(x, class = c("s2_geography", "wk_vctr"))
3: new_s2_geography(cpp_s2_rebuild(as_s2_geography(x), options))
4: s2::s2_rebuild(s2, s2_options)
5: st_make_valid.sfc(st_as_sfc(vec), s2_options = s2::s2_options(snap = s2::s2_snap_precision(1e+18)))
6: st_make_valid(st_as_sfc(vec), s2_options = s2::s2_options(snap = s2::s2_snap_precision(1e+18)))
An irrecoverable exception occurred. R is aborting now ...
==64930==
==64930== Process terminating with default action of signal 11 (SIGSEGV)
==64930== at 0x4D55A7C: __pthread_kill_implementation (pthread_kill.c:44)
==64930== by 0x4D55A7C: __pthread_kill_internal (pthread_kill.c:78)
==64930== by 0x4D55A7C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89)
==64930== by 0x4D01475: raise (raise.c:26)
==64930== by 0x4D0151F: ??? (in /usr/lib/x86_64-linux-gnu/libc.so.6)
==64930== by 0x1C7DDE5F: S2Builder::AddSnappedEdges(int, int, S2Builder::GraphOptions const&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >*, std::vector<int, std::allocator<int> >*, IdSetLexicon*, std::vector<gtl::compact_array<int, std::allocator<int> >, std::allocator<gtl::compact_array<int, std::allocator<int> > > >*) const (s2builder.cc:1169)
==64930==
==64930== HEAP SUMMARY:
==64930== in use at exit: 90,443,013 bytes in 68,393 blocks
==64930== total heap usage: 190,298 allocs, 121,905 frees, 212,859,421 bytes allocated
==64930==
==64930== LEAK SUMMARY:
==64930== definitely lost: 0 bytes in 0 blocks
==64930== indirectly lost: 0 bytes in 0 blocks
==64930== possibly lost: 1,010,801 bytes in 6,630 blocks
==64930== still reachable: 89,432,212 bytes in 61,763 blocks
==64930== of which reachable via heuristic:
==64930== length64 : 2,518,472 bytes in 23,992 blocks
==64930== newarray : 4,264 bytes in 1 blocks
==64930== suppressed: 0 bytes in 0 blocks
==64930== Rerun with --leak-check=full to see details of leaked memory
==64930==
==64930== For lists of detected and suppressed errors, rerun with: -s
==64930== ERROR SUMMARY: 17 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)