From fa5502100b11b2a1dfb832c3efd7ca7557b74b68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grisez?=
 <francois.grisez@belledonne-communications.com>
Date: Tue, 22 Jan 2019 14:44:09 +0100
Subject: [PATCH] Fix memory leak in the generated C++ wrapper

clone() methods sytematically took a reference on the freshly
created object whereas these had their reference counter set to one
already.

This commit prevents the wrapper generator take an extra reference
by passing false as 'takeRef' argument of cPtrToSharedPtr().

This commit also introduce a new property for abstractapi.Method
objects, which tells whether the method returns an allocated object.
---
 tools/abstractapi.py       | 4 ++++
 wrappers/cpp/genwrapper.py | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/abstractapi.py b/tools/abstractapi.py
index ab0573d964..35ec4ebeee 100644
--- a/tools/abstractapi.py
+++ b/tools/abstractapi.py
@@ -366,6 +366,10 @@ class Method(DocumentableObject):
 	def returnType(self, returnType):
 		self._returnType = returnType
 		returnType.parent = self
+
+	@property
+	def returnAllocatedObject(self):
+		return self.name.words[0] in ['create', 'new', 'clone']
 	
 	def translate_as_prototype(self, translator, **params):
 		return translator.translate_method_as_prototype(self, **params)
diff --git a/wrappers/cpp/genwrapper.py b/wrappers/cpp/genwrapper.py
index 6a87658dbe..26f2af8266 100755
--- a/wrappers/cpp/genwrapper.py
+++ b/wrappers/cpp/genwrapper.py
@@ -307,7 +307,7 @@ class CppTranslator(object):
 			if exprtype.desc.refcountable:
 				cppReturnType = CppTranslator.sharedPtrTypeExtractor.match(cppReturnType).group(2)
 
-				if isinstance(exprtype.parent, AbsApi.Method) and len(exprtype.parent.name.words) >=1 and (exprtype.parent.name.words[0] == 'create' or exprtype.parent.name.words[0] == 'new'):
+				if isinstance(exprtype.parent, AbsApi.Method) and len(exprtype.parent.name.words) >=1 and exprtype.parent.returnAllocatedObject:
 					return 'Object::cPtrToSharedPtr<{0}>({1}, false)'.format(cppReturnType, cExpr)
 				else:
 					return 'Object::cPtrToSharedPtr<{0}>({1})'.format(cppReturnType, cExpr)
-- 
GitLab