Page 1 of 1

Array<string> destructor incurs an exception

Posted: Fri Feb 16, 2018 3:52 am
by jackzhao123
Dear All:

Code: Select all

int ny = osimModel.getNumStateVariables();
Array<std::string> statNames("", ny);
statNames = osimModel.getStateVariableNames();
The above code is in a mex function called within MATLAB .

When this code is implemented after multiple (certain amount of) times, the Array<string> destructor will incur an exception at the following line when the whole program finishes at '}':

Code: Select all

virtual ~Array()
{
	if(_array!=NULL) { delete[] _array;  _array = NULL; }   //exception happens here
}
Whether it is caused by two pointers that point to the same memory space, while one in the "OpenSim::Model object" has been destructed but the other in "statNames" array object still intends to delete the space ? How should we solve it?

Best regards
Jack

Re: Array<string> destructor incurs an exception

Posted: Tue Feb 20, 2018 1:23 pm
by tkuchida
Do you get the same exception if the code is run without being embedded in a mex function? It might help your debugging efforts to know whether the issue is specific to the mex function implementation.

Re: Array<string> destructor incurs an exception

Posted: Fri Feb 23, 2018 10:12 pm
by jackzhao123
tkuchida wrote:Do you get the same exception if the code is run without being embedded in a mex function? It might help your debugging efforts to know whether the issue is specific to the mex function implementation.
Dear Tom:
Thanks so much for the reply, and I will go on try it accordingly.

Regards

Re: Array<string> destructor incurs an exception

Posted: Fri Feb 23, 2018 11:40 pm
by tkuchida
By the way, OpenSim can be accessed in MATLAB via the scripting interface (see the "Scripting with Matlab" page in the Confluence documentation, https://simtk-confluence.stanford.edu:8 ... ith+Matlab), which may be smoother than using mex functions.

Re: Array<string> destructor incurs an exception

Posted: Thu Jul 19, 2018 9:02 am
by jackzhao123
tkuchida wrote:
Fri Feb 23, 2018 11:40 pm
By the way, OpenSim can be accessed in MATLAB via the scripting interface (see the "Scripting with Matlab" page in the Confluence documentation, https://simtk-confluence.stanford.edu:8 ... ith+Matlab), which may be smoother than using mex functions.
Dear Thomas:
Thank you so much for your reply! While this problem in MATLAB still exists. When the mex function end after '}' of the function, the Array object will incur error causing MATLAB to end. I have tried multiple methods but has yet solved it.

Best regards
Jack

Re: Array<string> destructor incurs an exception

Posted: Fri Jul 20, 2018 12:16 pm
by tkuchida
When the mex function end after '}' of the function, the Array object will incur error causing MATLAB to end. I have tried multiple methods but has yet solved it.
It will be very difficult for someone else to debug your mex function without diving into your code. In your original post, you stated that "When this code is implemented after multiple (certain amount of) times...". I would have some hypotheses if the exception were always thrown on the second iteration, but if it's thrown after "multiple" iterations, it's somewhat more nebulous. Perhaps a race condition where destructors are called in an unexpected order? To circumvent the issue, you might try calling getStateVariableNames() and then immediately transferring the data into your own container.