Skip to content

Commit 6cec69e

Browse files
committed
Remove an unnecessary shared_ptr reference count increment in Ruby wrappers
When wrapping STL containers, remove a shared_ptr reference count increment when an upcast is needed when checking type conversion in traits_check::check.
1 parent 67a3858 commit 6cec69e

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Lib/ruby/std_shared_ptr.i

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@ namespace swig {
1313
template <class Type>
1414
struct traits_asptr<std::shared_ptr<Type> > {
1515
static int asptr(VALUE obj, std::shared_ptr<Type> **val) {
16-
std::shared_ptr<Type> *p = 0;
16+
int res = SWIG_ERROR;
1717
swig_type_info *descriptor = type_info<std::shared_ptr<Type> >();
18-
swig_ruby_owntype newmem = {0, 0};
19-
int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
20-
if (SWIG_IsOK(res)) {
21-
if (val) {
22-
if (*val) {
23-
**val = p ? *p : std::shared_ptr<Type>();
24-
} else {
25-
*val = p;
26-
if (newmem.own & SWIG_CAST_NEW_MEMORY) {
27-
// Upcast for pointers to shared_ptr in this generic framework has not been implemented
28-
res = SWIG_ERROR;
29-
}
30-
}
31-
}
32-
if (newmem.own & SWIG_CAST_NEW_MEMORY)
33-
delete p;
18+
if (val) {
19+
std::shared_ptr<Type> *p = 0;
20+
swig_ruby_owntype newmem = {0, 0};
21+
res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
22+
if (SWIG_IsOK(res)) {
23+
if (*val) {
24+
**val = p ? *p : std::shared_ptr<Type>();
25+
} else {
26+
*val = p;
27+
if (newmem.own & SWIG_CAST_NEW_MEMORY) {
28+
// Upcast for pointers to shared_ptr in this generic framework has not been implemented
29+
res = SWIG_ERROR;
30+
}
31+
}
32+
if (newmem.own & SWIG_CAST_NEW_MEMORY)
33+
delete p;
34+
}
35+
} else {
36+
res = descriptor ? SWIG_ConvertPtr(obj, 0, descriptor, 0) : SWIG_ERROR;
3437
}
3538
return res;
3639
}

0 commit comments

Comments
 (0)