diff --git a/sources/jvxLibraries/jvxLexternalCall/include/CjvxMexCallsTpl.h b/sources/jvxLibraries/jvxLexternalCall/include/CjvxMexCallsTpl.h index 7df89cfb..ebacb6b4 100644 --- a/sources/jvxLibraries/jvxLexternalCall/include/CjvxMexCallsTpl.h +++ b/sources/jvxLibraries/jvxLexternalCall/include/CjvxMexCallsTpl.h @@ -16,7 +16,10 @@ class CjvxMexCallsTpl : public CjvxMexCalls, public T res = T::select(theOwner); if (res == JVX_NO_ERROR) { + // Run the class specific initialization initExternalCall(); + + // Run mex call base classes function to init matlab call handles res = CjvxMexCalls::select(_common_set_min.theHostRef, static_cast(this), "", _theExtCallObjectName.c_str()); } @@ -26,13 +29,22 @@ class CjvxMexCallsTpl : public CjvxMexCalls, public T jvxErrorType unselect() override { jvxErrorType res = JVX_NO_ERROR; - res = T::unselect(); \ - if (res == JVX_NO_ERROR) - { - res = CjvxMexCalls::unselect(); - terminateExternalCall(); - _theExtCallObjectName = ""; - } + res = T::unselect(); + if (res == JVX_NO_ERROR) + { + // Run the class specific implementation + terminateExternalCall(); + res = CjvxMexCalls::unselect(); + + /* + * Should have been removed already in + returnExternalCallHandle(_common_set_min.theHostRef, parent._theExtCallHandler); + parent._theExtCallHandler = NULL; + parent._theExtCallObjectName = ""; + */ + + _theExtCallObjectName = ""; + } return(res); }; diff --git a/sources/jvxLibraries/jvxLexternalCall/src/CjvxMexCalls_prv.cpp b/sources/jvxLibraries/jvxLexternalCall/src/CjvxMexCalls_prv.cpp index ab2fd095..d1697c17 100644 --- a/sources/jvxLibraries/jvxLexternalCall/src/CjvxMexCalls_prv.cpp +++ b/sources/jvxLibraries/jvxLexternalCall/src/CjvxMexCalls_prv.cpp @@ -29,13 +29,19 @@ CjvxMexCalls_prv::select(IjvxHiddenInterface* theHost, CjvxProperties* theProps, { #ifdef JVX_EXTERNAL_CALL_ENABLED - if (componentName) - parent._theExtCallObjectName = componentName; + if (parent._theExtCallObjectName.empty()) + { + if (componentName) + parent._theExtCallObjectName = componentName; + } - parent._theExtCallHandler = NULL; - requestExternalCallHandle(theHost, &parent._theExtCallHandler); + if (parent._theExtCallHandler == nullptr) + { + requestExternalCallHandle(theHost, &parent._theExtCallHandler); + } + if (!parent._theExtCallHandler) - std::cerr << "[CjvxMexCalls_prv] WARNING: could not obtain mex call handler, disabling external call interface" << std::endl; + std::cerr << "[CjvxMexCalls_prv] WARNING: could not obtain mex call handler, disabling external call interface." << std::endl; initExternalCallhandler(parent._theExtCallHandler, static_cast(this), parent._theExtCallObjectName); runtime.idThread = JVX_GET_CURRENT_THREAD_ID(); @@ -54,8 +60,18 @@ jvxErrorType CjvxMexCalls_prv::unselect() { #ifdef JVX_EXTERNAL_CALL_ENABLED - terminateExternalCallhandler(parent._theExtCallHandler); - returnExternalCallHandle(theHostRef, parent._theExtCallHandler); + + if (parent._theExtCallHandler) + { + + terminateExternalCallhandler(parent._theExtCallHandler); + returnExternalCallHandle(theHostRef, parent._theExtCallHandler); + } + else + { + std::cerr << "[CjvxMexCalls_prv] WARNING: mex call handler removed to early, I am not able to de-regsiter the callbacks as required!" << std::endl; + } + parent._theExtCallHandler = NULL; parent._theExtCallObjectName = "";