Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper fixes for Matlab memory leak #970

Merged
merged 3 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wrap/gtwrap/matlab_wrapper/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WrapperTemplate:
mxDestroyArray(registry);

mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
if(mexPutVariable("global", "gtsam_geometry_rttiRegistry_created", newAlreadyCreated) != 0) {{
if(mexPutVariable("global", "gtsam_{module_name}_rttiRegistry_created", newAlreadyCreated) != 0) {{
mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
}}
mxDestroyArray(newAlreadyCreated);
Expand Down
6 changes: 3 additions & 3 deletions wrap/gtwrap/matlab_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

# pylint: disable=too-many-lines, no-self-use, too-many-arguments, too-many-branches, too-many-statements

import copy
import os
import os.path as osp
import textwrap
from functools import partial, reduce
from typing import Dict, Iterable, List, Union
import copy

import gtwrap.interface_parser as parser
from gtwrap.interface_parser.function import ArgumentList
import gtwrap.template_instantiator as instantiator
from gtwrap.interface_parser.function import ArgumentList
from gtwrap.matlab_wrapper.mixins import CheckMixin, FormatMixin
from gtwrap.matlab_wrapper.templates import WrapperTemplate

Expand Down Expand Up @@ -1269,9 +1269,9 @@ def generate_collector_function(self, func_id):
Collector_{class_name}::iterator item;
item = collector_{class_name}.find(self);
if(item != collector_{class_name}.end()) {{
delete self;
collector_{class_name}.erase(item);
}}
delete self;
''').format(class_name_sep=class_name_separated,
class_name=class_name),
prefix=' ')
Expand Down
21 changes: 14 additions & 7 deletions wrap/gtwrap/pybind_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,20 @@ def _wrap_method(self,
prefix,
suffix,
method_suffix=""):
"""
Wrap the `method` for the class specified by `cpp_class`.

Args:
method: The method to wrap.
cpp_class: The C++ name of the class to which the method belongs.
prefix: Prefix to add to the wrapped method when writing to the cpp file.
suffix: Suffix to add to the wrapped method when writing to the cpp file.
method_suffix: A string to append to the wrapped method name.
"""
py_method = method.name + method_suffix
cpp_method = method.to_cpp()

# Special handling for the serialize/serializable method
if cpp_method in ["serialize", "serializable"]:
if not cpp_class in self._serializing_classes:
self._serializing_classes.append(cpp_class)
Expand All @@ -104,16 +115,12 @@ def _wrap_method(self,
'.def("deserialize", []({class_inst} self, string serialized)' \
'{{ gtsam::deserialize(serialized, *self); }}, py::arg("serialized"))' \
.format(class_inst=cpp_class + '*')
return serialize_method + deserialize_method

if cpp_method == "pickle":
if not cpp_class in self._serializing_classes:
raise ValueError(
"Cannot pickle a class which is not serializable")
# Since this class supports serialization, we also add the pickle method.
pickle_method = self.method_indent + \
".def(py::pickle({indent} [](const {cpp_class} &a){{ /* __getstate__: Returns a string that encodes the state of the object */ return py::make_tuple(gtsam::serialize(a)); }},{indent} [](py::tuple t){{ /* __setstate__ */ {cpp_class} obj; gtsam::deserialize(t[0].cast<std::string>(), obj); return obj; }}))"
return pickle_method.format(cpp_class=cpp_class,
indent=self.method_indent)
return serialize_method + deserialize_method + \
pickle_method.format(cpp_class=cpp_class, indent=self.method_indent)

# Add underscore to disambiguate if the method name matches a python keyword
if py_method in self.python_keywords:
Expand Down
24 changes: 12 additions & 12 deletions wrap/tests/expected/matlab/class_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void _class_RTTIRegister() {
mxDestroyArray(registry);

mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
if(mexPutVariable("global", "gtsam_geometry_rttiRegistry_created", newAlreadyCreated) != 0) {
if(mexPutVariable("global", "gtsam_class_rttiRegistry_created", newAlreadyCreated) != 0) {
mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
}
mxDestroyArray(newAlreadyCreated);
Expand Down Expand Up @@ -180,9 +180,9 @@ void FunRange_deconstructor_2(int nargout, mxArray *out[], int nargin, const mxA
Collector_FunRange::iterator item;
item = collector_FunRange.find(self);
if(item != collector_FunRange.end()) {
delete self;
collector_FunRange.erase(item);
}
delete self;
}

void FunRange_range_3(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -216,9 +216,9 @@ void FunDouble_deconstructor_6(int nargout, mxArray *out[], int nargin, const mx
Collector_FunDouble::iterator item;
item = collector_FunDouble.find(self);
if(item != collector_FunDouble.end()) {
delete self;
collector_FunDouble.erase(item);
}
delete self;
}

void FunDouble_multiTemplatedMethod_7(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -301,9 +301,9 @@ void Test_deconstructor_15(int nargout, mxArray *out[], int nargin, const mxArra
Collector_Test::iterator item;
item = collector_Test.find(self);
if(item != collector_Test.end()) {
delete self;
collector_Test.erase(item);
}
delete self;
}

void Test_arg_EigenConstRef_16(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -544,9 +544,9 @@ void PrimitiveRefDouble_deconstructor_43(int nargout, mxArray *out[], int nargin
Collector_PrimitiveRefDouble::iterator item;
item = collector_PrimitiveRefDouble.find(self);
if(item != collector_PrimitiveRefDouble.end()) {
delete self;
collector_PrimitiveRefDouble.erase(item);
}
delete self;
}

void PrimitiveRefDouble_Brutal_44(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -584,9 +584,9 @@ void MyVector3_deconstructor_47(int nargout, mxArray *out[], int nargin, const m
Collector_MyVector3::iterator item;
item = collector_MyVector3.find(self);
if(item != collector_MyVector3.end()) {
delete self;
collector_MyVector3.erase(item);
}
delete self;
}

void MyVector12_collectorInsertAndMakeBase_48(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -617,9 +617,9 @@ void MyVector12_deconstructor_50(int nargout, mxArray *out[], int nargin, const
Collector_MyVector12::iterator item;
item = collector_MyVector12.find(self);
if(item != collector_MyVector12.end()) {
delete self;
collector_MyVector12.erase(item);
}
delete self;
}

void MultipleTemplatesIntDouble_collectorInsertAndMakeBase_51(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand All @@ -639,9 +639,9 @@ void MultipleTemplatesIntDouble_deconstructor_52(int nargout, mxArray *out[], in
Collector_MultipleTemplatesIntDouble::iterator item;
item = collector_MultipleTemplatesIntDouble.find(self);
if(item != collector_MultipleTemplatesIntDouble.end()) {
delete self;
collector_MultipleTemplatesIntDouble.erase(item);
}
delete self;
}

void MultipleTemplatesIntFloat_collectorInsertAndMakeBase_53(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand All @@ -661,9 +661,9 @@ void MultipleTemplatesIntFloat_deconstructor_54(int nargout, mxArray *out[], int
Collector_MultipleTemplatesIntFloat::iterator item;
item = collector_MultipleTemplatesIntFloat.find(self);
if(item != collector_MultipleTemplatesIntFloat.end()) {
delete self;
collector_MultipleTemplatesIntFloat.erase(item);
}
delete self;
}

void ForwardKinematics_collectorInsertAndMakeBase_55(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -714,9 +714,9 @@ void ForwardKinematics_deconstructor_58(int nargout, mxArray *out[], int nargin,
Collector_ForwardKinematics::iterator item;
item = collector_ForwardKinematics.find(self);
if(item != collector_ForwardKinematics.end()) {
delete self;
collector_ForwardKinematics.erase(item);
}
delete self;
}

void TemplatedConstructor_collectorInsertAndMakeBase_59(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -783,9 +783,9 @@ void TemplatedConstructor_deconstructor_64(int nargout, mxArray *out[], int narg
Collector_TemplatedConstructor::iterator item;
item = collector_TemplatedConstructor.find(self);
if(item != collector_TemplatedConstructor.end()) {
delete self;
collector_TemplatedConstructor.erase(item);
}
delete self;
}

void MyFactorPosePoint2_collectorInsertAndMakeBase_65(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -820,9 +820,9 @@ void MyFactorPosePoint2_deconstructor_67(int nargout, mxArray *out[], int nargin
Collector_MyFactorPosePoint2::iterator item;
item = collector_MyFactorPosePoint2.find(self);
if(item != collector_MyFactorPosePoint2.end()) {
delete self;
collector_MyFactorPosePoint2.erase(item);
}
delete self;
}

void MyFactorPosePoint2_print_68(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down
2 changes: 1 addition & 1 deletion wrap/tests/expected/matlab/functions_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void _functions_RTTIRegister() {
mxDestroyArray(registry);

mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
if(mexPutVariable("global", "gtsam_geometry_rttiRegistry_created", newAlreadyCreated) != 0) {
if(mexPutVariable("global", "gtsam_functions_rttiRegistry_created", newAlreadyCreated) != 0) {
mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
}
mxDestroyArray(newAlreadyCreated);
Expand Down
4 changes: 2 additions & 2 deletions wrap/tests/expected/matlab/geometry_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void gtsamPoint2_deconstructor_3(int nargout, mxArray *out[], int nargin, const
Collector_gtsamPoint2::iterator item;
item = collector_gtsamPoint2.find(self);
if(item != collector_gtsamPoint2.end()) {
delete self;
collector_gtsamPoint2.erase(item);
}
delete self;
}

void gtsamPoint2_argChar_4(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -262,9 +262,9 @@ void gtsamPoint3_deconstructor_20(int nargout, mxArray *out[], int nargin, const
Collector_gtsamPoint3::iterator item;
item = collector_gtsamPoint3.find(self);
if(item != collector_gtsamPoint3.end()) {
delete self;
collector_gtsamPoint3.erase(item);
}
delete self;
}

void gtsamPoint3_norm_21(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down
10 changes: 5 additions & 5 deletions wrap/tests/expected/matlab/inheritance_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void _inheritance_RTTIRegister() {
mxDestroyArray(registry);

mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
if(mexPutVariable("global", "gtsam_geometry_rttiRegistry_created", newAlreadyCreated) != 0) {
if(mexPutVariable("global", "gtsam_inheritance_rttiRegistry_created", newAlreadyCreated) != 0) {
mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
}
mxDestroyArray(newAlreadyCreated);
Expand Down Expand Up @@ -121,9 +121,9 @@ void MyBase_deconstructor_2(int nargout, mxArray *out[], int nargin, const mxArr
Collector_MyBase::iterator item;
item = collector_MyBase.find(self);
if(item != collector_MyBase.end()) {
delete self;
collector_MyBase.erase(item);
}
delete self;
}

void MyTemplatePoint2_collectorInsertAndMakeBase_3(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -171,9 +171,9 @@ void MyTemplatePoint2_deconstructor_6(int nargout, mxArray *out[], int nargin, c
Collector_MyTemplatePoint2::iterator item;
item = collector_MyTemplatePoint2.find(self);
if(item != collector_MyTemplatePoint2.end()) {
delete self;
collector_MyTemplatePoint2.erase(item);
}
delete self;
}

void MyTemplatePoint2_accept_T_7(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -339,9 +339,9 @@ void MyTemplateMatrix_deconstructor_22(int nargout, mxArray *out[], int nargin,
Collector_MyTemplateMatrix::iterator item;
item = collector_MyTemplateMatrix.find(self);
if(item != collector_MyTemplateMatrix.end()) {
delete self;
collector_MyTemplateMatrix.erase(item);
}
delete self;
}

void MyTemplateMatrix_accept_T_23(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -492,9 +492,9 @@ void ForwardKinematicsFactor_deconstructor_37(int nargout, mxArray *out[], int n
Collector_ForwardKinematicsFactor::iterator item;
item = collector_ForwardKinematicsFactor.find(self);
if(item != collector_ForwardKinematicsFactor.end()) {
delete self;
collector_ForwardKinematicsFactor.erase(item);
}
delete self;
}


Expand Down
8 changes: 4 additions & 4 deletions wrap/tests/expected/matlab/multiple_files_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void _multiple_files_RTTIRegister() {
mxDestroyArray(registry);

mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
if(mexPutVariable("global", "gtsam_geometry_rttiRegistry_created", newAlreadyCreated) != 0) {
if(mexPutVariable("global", "gtsam_multiple_files_rttiRegistry_created", newAlreadyCreated) != 0) {
mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
}
mxDestroyArray(newAlreadyCreated);
Expand Down Expand Up @@ -110,9 +110,9 @@ void gtsamClass1_deconstructor_2(int nargout, mxArray *out[], int nargin, const
Collector_gtsamClass1::iterator item;
item = collector_gtsamClass1.find(self);
if(item != collector_gtsamClass1.end()) {
delete self;
collector_gtsamClass1.erase(item);
}
delete self;
}

void gtsamClass2_collectorInsertAndMakeBase_3(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -143,9 +143,9 @@ void gtsamClass2_deconstructor_5(int nargout, mxArray *out[], int nargin, const
Collector_gtsamClass2::iterator item;
item = collector_gtsamClass2.find(self);
if(item != collector_gtsamClass2.end()) {
delete self;
collector_gtsamClass2.erase(item);
}
delete self;
}

void gtsamClassA_collectorInsertAndMakeBase_6(int nargout, mxArray *out[], int nargin, const mxArray *in[])
Expand Down Expand Up @@ -176,9 +176,9 @@ void gtsamClassA_deconstructor_8(int nargout, mxArray *out[], int nargin, const
Collector_gtsamClassA::iterator item;
item = collector_gtsamClassA.find(self);
if(item != collector_gtsamClassA.end()) {
delete self;
collector_gtsamClassA.erase(item);
}
delete self;
}


Expand Down
Loading