diff --git a/compile/customWrapper/classHandle.hpp b/compile/customWrapper/classHandle.hpp index a3728f28c..f83417425 100644 --- a/compile/customWrapper/classHandle.hpp +++ b/compile/customWrapper/classHandle.hpp @@ -60,9 +60,9 @@ template inline std::string convertPtr2String(base *ptr) return std::to_string(handle); } -template inline ClassHandle *convertString2HandlePtr(const char* in) +template inline ClassHandle *convertString2HandlePtr(const char* in, int size) { - uint64_t value = stoull(std::string(in)); + uint64_t value = stoull(std::string(in).substr(0, size)); ClassHandle *ptr = reinterpret_cast *>(value); if (!ptr->isValid()) throw std::invalid_argument("callback handle is not valid"); diff --git a/compile/customWrapper/wrapperMex.cpp b/compile/customWrapper/wrapperMex.cpp index 547406992..af0f3c76e 100644 --- a/compile/customWrapper/wrapperMex.cpp +++ b/compile/customWrapper/wrapperMex.cpp @@ -9,9 +9,9 @@ template inline mxArray *convertPtr2Mat(base *ptr) return mxCreateString(convertPtr2String(ptr).c_str()); } -template inline void destroyObject(const char* in) +template inline void destroyObject(const char* in, size_t size) { - delete convertString2HandlePtr(in); + delete convertString2HandlePtr(in, size); mexUnlock(); } @@ -51,7 +51,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) if (!strcmp("delete", cmd)) { if (nrhs != 2) mexErrMsgTxt("Second input for delete command should be a class instance handle."); - destroyObject(getStringFromMxArray(prhs[1], "class instance handle should a row vector char array")); + size_t size = mxGetNumberOfElements(prhs[1]); + destroyObject(getStringFromMxArray(prhs[1], "class instance handle should be a row vector char array"), size); if (nlhs != 0 || nrhs != 2) mexWarnMsgTxt("Delete: Unexpected arguments ignored."); return; diff --git a/targetFunctions/common/customModelFunctions/callCppFunction.m b/targetFunctions/common/customModelFunctions/callCppFunction.m index 42d36a8bd..fbefd6e0e 100644 --- a/targetFunctions/common/customModelFunctions/callCppFunction.m +++ b/targetFunctions/common/customModelFunctions/callCppFunction.m @@ -8,7 +8,7 @@ coder.cinclude('classHandle.hpp') callbackHandle = coder.opaque('ClassHandle *','NULL'); - callbackHandle = coder.ceval('convertString2HandlePtr', pointer); + callbackHandle = coder.ceval('convertString2HandlePtr', pointer, numel(pointer)); callback = coder.opaque('CallbackInterface *','NULL'); callback = coder.ceval('std::mem_fn(&ClassHandle::ptr)', callbackHandle);