diff --git a/django/contrib/gis/geos/prepared.py b/django/contrib/gis/geos/prepared.py
index a6a205d..d5b531b 100644
a
|
b
|
class PreparedGeometry(GEOSBase):
|
12 | 12 | ptr_type = capi.PREPGEOM_PTR |
13 | 13 | |
14 | 14 | def __init__(self, geom): |
| 15 | # Keeping a reference to the original geometry object to prevent it |
| 16 | # from being garbage collected which could then crash the prepared one |
| 17 | # See #21662 |
| 18 | self._base_geom = geom |
15 | 19 | if not isinstance(geom, GEOSGeometry): |
16 | 20 | raise TypeError |
17 | 21 | self.ptr = capi.geos_prepare(geom.ptr) |
diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
index 3f0ce06..9ce7e0d 100644
a
|
b
|
class GEOSTest(unittest.TestCase, TestDataMixin):
|
1046 | 1046 | self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) |
1047 | 1047 | self.assertEqual(c, prep.covers(pnt)) |
1048 | 1048 | |
| 1049 | # Original geometry deletion should not crash the prepared one (#21662) |
| 1050 | del mpoly |
| 1051 | self.assertTrue(prep.covers(Point(5, 5))) |
| 1052 | |
1049 | 1053 | def test_line_merge(self): |
1050 | 1054 | "Testing line merge support" |
1051 | 1055 | ref_geoms = (fromstr('LINESTRING(1 1, 1 1, 3 3)'), |