From 364fa82499161a66c9b46cb721737f75237cf5cd Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sun, 18 Dec 2016 19:53:20 +0000 Subject: [PATCH] Replicate Python memory leak fix in std::pair for Octave and Ruby https://github.com/swig/swig/pull/851 --- CHANGES.current | 4 ++++ Lib/octave/std_pair.i | 8 ++++++-- Lib/python/std_pair.i | 10 ++++------ Lib/ruby/std_pair.i | 14 ++++++++++---- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 81ea4ac7e..1695cb216 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.11 (in progress) ============================ +2016-12-18: ezralanglois + [Python, Ruby, Octave] Memory leak fix on error in std::pair wrappers. + Issue https://github.com/swig/swig/pull/851 + 2016-12-16: bcaine [Guile] Patch #744 Added support for Guile's native pointer functionality diff --git a/Lib/octave/std_pair.i b/Lib/octave/std_pair.i index a06498bf2..2f3d4dfa4 100644 --- a/Lib/octave/std_pair.i +++ b/Lib/octave/std_pair.i @@ -69,12 +69,16 @@ value_type *vp = %new_instance(std::pair); T *pfirst = &(vp->first); int res1 = swig::asval(first, pfirst); - if (!SWIG_IsOK(res1)) + if (!SWIG_IsOK(res1)) { + %delete(vp); return res1; + } U *psecond = &(vp->second); int res2 = swig::asval(second, psecond); - if (!SWIG_IsOK(res2)) + if (!SWIG_IsOK(res2)) { + %delete(vp); return res2; + } *val = vp; return SWIG_AddNewMask(res1 > res2 ? res1 : res2); } else { diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index 0fcd4f082..172572bff 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -31,7 +31,7 @@ int res2 = swig::asval((PyObject*)second, psecond); if (!SWIG_IsOK(res2)) return res2; return res1 > res2 ? res1 : res2; - } + } } static int asval(PyObject *obj, std::pair *val) { @@ -68,15 +68,13 @@ value_type *vp = %new_instance(std::pair); T *pfirst = &(vp->first); int res1 = swig::asval((PyObject*)first, pfirst); - if (!SWIG_IsOK(res1)) - { + if (!SWIG_IsOK(res1)) { %delete(vp); return res1; } U *psecond = &(vp->second); int res2 = swig::asval((PyObject*)second, psecond); - if (!SWIG_IsOK(res2)) - { + if (!SWIG_IsOK(res2)) { %delete(vp); return res2; } @@ -90,7 +88,7 @@ int res2 = swig::asval((PyObject*)second, psecond); if (!SWIG_IsOK(res2)) return res2; return res1 > res2 ? res1 : res2; - } + } } static int asptr(PyObject *obj, std::pair **val) { diff --git a/Lib/ruby/std_pair.i b/Lib/ruby/std_pair.i index 5bea67c7c..38a4ffb9b 100644 --- a/Lib/ruby/std_pair.i +++ b/Lib/ruby/std_pair.i @@ -31,7 +31,7 @@ int res2 = swig::asval((VALUE)second, psecond); if (!SWIG_IsOK(res2)) return res2; return res1 > res2 ? res1 : res2; - } + } } static int asval(VALUE obj, std::pair *val) { @@ -63,10 +63,16 @@ value_type *vp = %new_instance(std::pair); T *pfirst = &(vp->first); int res1 = swig::asval((VALUE)first, pfirst); - if (!SWIG_IsOK(res1)) return res1; + if (!SWIG_IsOK(res1)) { + %delete(vp); + return res1; + } U *psecond = &(vp->second); int res2 = swig::asval((VALUE)second, psecond); - if (!SWIG_IsOK(res2)) return res2; + if (!SWIG_IsOK(res2)) { + %delete(vp); + return res2; + } *val = vp; return SWIG_AddNewMask(res1 > res2 ? res1 : res2); } else { @@ -77,7 +83,7 @@ int res2 = swig::asval((VALUE)second, psecond); if (!SWIG_IsOK(res2)) return res2; return res1 > res2 ? res1 : res2; - } + } } static int asptr(VALUE obj, std::pair **val) {