@@ -26,7 +26,6 @@ struct pyMOOSException : std::exception {
2626 }
2727 char const * what () const throw () {
2828 return s_.c_str ();
29- return " lala" ;/* return s_.c_str();*/
3029 }
3130 std::string s_;
3231};
@@ -45,6 +44,10 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
4544private:
4645 typedef MOOSAsyncCommClient BASE;
4746public:
47+
48+ ~AsyncCommsWrapper (){
49+ Close (true );
50+ }
4851
4952 bool Run (const std::string & sServer , int Port, const std::string & sMyName ) {
5053 return BASE::Run (sServer , Port, sMyName , 0 );// Comms Tick not used in Async version
@@ -81,13 +84,14 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
8184 return true ;
8285 }
8386
84- bool Close (bool nice)
85- {
86- Py_BEGIN_ALLOW_THREADS
87- // PyGILState_STATE gstate = PyGILState_Ensure();
88- BASE::Close (nice);
89- // PyGILState_Release(gstate);
90- Py_END_ALLOW_THREADS
87+ bool Close (bool nice){
88+ Py_BEGIN_ALLOW_THREADS
89+ // PyGILState_STATE gstate = PyGILState_Ensure();
90+ closing_ = true ;
91+ BASE::Close (true );
92+
93+ // PyGILState_Release(gstate);
94+ Py_END_ALLOW_THREADS
9195 }
9296
9397
@@ -124,13 +128,14 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
124128 }
125129
126130 bool on_mail () {
127-
128131 bool bResult = false ;
129132
130133 PyGILState_STATE gstate = PyGILState_Ensure ();
131134 try {
132- bp::object result = on_mail_object_ ();
133- bResult = bp::extract<bool >(result);
135+ if (!closing_){
136+ bp::object result = on_mail_object_ ();
137+ bResult = bp::extract<bool >(result);
138+ }
134139 } catch (const bp::error_already_set& e) {
135140 PyGILState_Release (gstate);
136141 throw pyMOOSException (
@@ -140,7 +145,6 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
140145 PyGILState_Release (gstate);
141146
142147 return bResult;
143-
144148 }
145149
146150 static bool active_queue_delegate (CMOOSMsg & M, void * pParam) {
@@ -209,7 +213,9 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
209213 /* * callback functions (stored) */
210214 bp::object on_connect_object_;
211215 bp::object on_mail_object_;
212-
216+
217+ /* * close connection flag */
218+ bool closing_;
213219};
214220}
215221;// namesapce
0 commit comments