Skip to content

Commit 657c5e4

Browse files
authored
Merge pull request OSGeo#13348 from dbaston/swig-feature-get-defn-refcount
SWIG: Increment FeatureDefn refcount on Feature.GetDefnRef
2 parents ea75dfb + 43287e8 commit 657c5e4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

autotest/ogr/ogr_basic_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,41 @@ def test_feature_use_after_destroy():
11301130
feature.DumpReadable()
11311131

11321132

1133+
def test_feature_defn_use_after_feature_delete():
1134+
1135+
defn = ogr.FeatureDefn("mydef")
1136+
feature = ogr.Feature(defn)
1137+
del defn
1138+
1139+
defn2 = feature.GetDefnRef()
1140+
del feature
1141+
1142+
assert defn2.GetName() == "mydef"
1143+
1144+
1145+
def test_layer_get_defn_refcount():
1146+
1147+
ds = ogr.Open("data/poly.shp")
1148+
lyr = ds.GetLayer(0)
1149+
1150+
for i in range(10):
1151+
defn = lyr.GetLayerDefn()
1152+
1153+
assert defn.GetReferenceCount() == 2
1154+
1155+
1156+
def test_feature_get_defn_refcount():
1157+
1158+
ds = ogr.Open("data/poly.shp")
1159+
lyr = ds.GetLayer(0)
1160+
feature = lyr.GetNextFeature()
1161+
1162+
for i in range(10):
1163+
defn = feature.GetDefnRef()
1164+
1165+
assert defn.GetReferenceCount() == 3
1166+
1167+
11331168
@pytest.mark.parametrize("destroy_method", ("del", "Destroy"))
11341169
def test_geom_use_after_feature_delete_1(destroy_method):
11351170

swig/include/ogr.i

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,12 @@ public:
18961896
return (OGRFeatureShadow*) OGR_F_Create( feature_def );
18971897
}
18981898

1899+
%newobject GetDefnRef;
18991900
OGRFeatureDefnShadow *GetDefnRef() {
1900-
return (OGRFeatureDefnShadow*) OGR_F_GetDefnRef(self);
1901+
auto defn = (OGRFeatureDefnShadow*) OGR_F_GetDefnRef(self);
1902+
if (defn)
1903+
OGR_FD_Reference(defn);
1904+
return defn;
19011905
}
19021906

19031907
OGRErr SetGeometry(OGRGeometryShadow* geom) {

0 commit comments

Comments
 (0)