Skip to content

Commit 339d427

Browse files
committed
Merge branch 'master-fix-vector-shared_ptr'
* master-fix-vector-shared_ptr: fixing memleak of shared_ptr objects in python with creating a [wrapped] vector<shared_ptr<Foo>> from a list of shared_ptr<Bar> where Bar is derived from Foo.
2 parents e3524be + af97d8f commit 339d427

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Lib/python/pystdcommon.swg

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,23 @@ namespace swig {
4545
template <class Type>
4646
struct traits_asptr {
4747
static int asptr(PyObject *obj, Type **val) {
48-
Type *p = 0;
49-
swig_type_info *descriptor = type_info<Type>();
50-
int res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR;
51-
if (SWIG_IsOK(res)) {
52-
if (val) *val = p;
48+
if (val) {
49+
Type *p = 0;
50+
swig_type_info *descriptor = type_info<Type>();
51+
int newmem = 0;
52+
int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
53+
if (SWIG_IsOK(res)) {
54+
if (newmem & SWIG_CAST_NEW_MEMORY) {
55+
res |= SWIG_NEWOBJMASK;
56+
}
57+
*val = p;
58+
}
59+
return res;
60+
} else {
61+
swig_type_info *descriptor = type_info<Type>();
62+
int res = descriptor ? SWIG_ConvertPtr(obj, 0, descriptor, 0) : SWIG_ERROR;
63+
return res;
5364
}
54-
return res;
5565
}
5666
};
5767

0 commit comments

Comments
 (0)