Skip to content

Commit ca98e77

Browse files
committed
#2471 Fix refcount when setting aggregates
1 parent cbd9c89 commit ca98e77

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/ifcwrap/utils/type_conversion.i

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
PyObject* element = PySequence_GetItem(aggregate, i);
3636
// This is equivalent to the PyFloat_CheckExact macro. This means
3737
// that direct instances of int, float, str, etc. need to be used.
38-
if (element->ob_type != type_obj) return false;
38+
bool b = element->ob_type != type_obj;
39+
Py_DECREF(element);
40+
if (!b) {
41+
return false;
42+
}
3943
}
4044
return true;
4145
}
@@ -44,7 +48,9 @@
4448
if (!PySequence_Check(aggregate)) return false;
4549
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
4650
PyObject* element = PySequence_GetItem(aggregate, i);
47-
if (!check_aggregate_of_type(element, type_obj)) {
51+
bool b = check_aggregate_of_type(element, type_obj);
52+
Py_DECREF(element);
53+
if (!b) {
4854
return false;
4955
}
5056
}
@@ -99,6 +105,7 @@
99105
PyObject* element = PySequence_GetItem(aggregate, i);
100106
std::vector<T> t = python_sequence_as_vector<T>(element);
101107
result_vector.push_back(t);
108+
Py_DECREF(element);
102109
}
103110
return result_vector;
104111
}

src/ifcwrap/utils/typemaps_in.i

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
8080
PyObject* element = PySequence_GetItem($input, i);
8181
void *arg = 0;
8282
int res = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_IfcParse__declaration, 0);
83+
Py_DECREF(element);
8384
auto decl = static_cast<const IfcParse::declaration*>(SWIG_IsOK(res) ? arg : 0);
8485
if (decl) {
8586
$1->push_back(decl);
@@ -99,6 +100,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
99100
PyObject* element = PySequence_GetItem($input, i);
100101
void *arg = 0;
101102
int res = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_IfcParse__entity, 0);
103+
Py_DECREF(element);
102104
auto decl = static_cast<const IfcParse::entity*>(SWIG_IsOK(res) ? arg : 0);
103105
if (decl) {
104106
$1->push_back(decl);
@@ -118,6 +120,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
118120
PyObject* element = PySequence_GetItem($input, i);
119121
void *arg = 0;
120122
int res = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_IfcParse__attribute, 0);
123+
Py_DECREF(element);
121124
auto decl = static_cast<const IfcParse::attribute*>(SWIG_IsOK(res) ? arg : 0);
122125
if (decl) {
123126
$1->push_back(decl);
@@ -137,6 +140,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
137140
PyObject* element = PySequence_GetItem($input, i);
138141
void *arg = 0;
139142
int res = SWIG_ConvertPtr(element, &arg, SWIGTYPE_p_IfcParse__inverse_attribute, 0);
143+
Py_DECREF(element);
140144
auto decl = static_cast<const IfcParse::inverse_attribute*>(SWIG_IsOK(res) ? arg : 0);
141145
if (decl) {
142146
$1->push_back(decl);
@@ -155,6 +159,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
155159
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i) {
156160
PyObject* element = PySequence_GetItem($input, i);
157161
$1->push_back(PyObject_IsTrue(element));
162+
Py_DECREF(element);
158163
}
159164
} else {
160165
SWIG_exception(SWIG_TypeError, "Expected an sequence type");
@@ -167,6 +172,7 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
167172
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i) {
168173
PyObject* element = PySequence_GetItem($input, i);
169174
IfcUtil::IfcBaseClass* inst = cast_pyobject<IfcUtil::IfcBaseClass*>(element);
175+
Py_DECREF(element);
170176
if (inst) {
171177
$1->push(inst);
172178
} else {
@@ -183,23 +189,28 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str)
183189
$1 = aggregate_of_aggregate_of_instance::ptr(new aggregate_of_aggregate_of_instance());
184190
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i) {
185191
PyObject* element = PySequence_GetItem($input, i);
192+
bool b = false;
186193
if (PySequence_Check(element)) {
194+
b = true;
187195
std::vector<IfcUtil::IfcBaseClass*> vector;
188196
vector.reserve(PySequence_Size(element));
189197
for(Py_ssize_t j = 0; j < PySequence_Size(element); ++j) {
190198
PyObject* element_element = PySequence_GetItem(element, j);
191199
IfcUtil::IfcBaseClass* inst = cast_pyobject<IfcUtil::IfcBaseClass*>(element_element);
200+
Py_DECREF(element_element);
192201
if (inst) {
193-
vector.push_back(cast_pyobject<IfcUtil::IfcBaseClass*>(element_element));
202+
vector.push_back(inst);
194203
} else {
195204
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");
196-
}
205+
}
197206
}
198207
$1->push(vector);
199-
} else {
208+
}
209+
Py_DECREF(element);
210+
if (!b) {
200211
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");
201212
break;
202-
}
213+
}
203214
}
204215
} else {
205216
SWIG_exception(SWIG_TypeError, "Attribute of type AGGREGATE OF AGGREGATE OF ENTITY INSTANCE needs a python sequence of sequence of entity instances");

0 commit comments

Comments
 (0)