Array<string> destructor incurs an exception

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Jack Zhao
Posts: 94
Joined: Thu Jul 24, 2014 7:15 am

Array<string> destructor incurs an exception

Post by Jack Zhao » Fri Feb 16, 2018 3:52 am

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

User avatar
Thomas Uchida
Posts: 1778
Joined: Wed May 16, 2012 11:40 am

Re: Array<string> destructor incurs an exception

Post by Thomas Uchida » Tue Feb 20, 2018 1:23 pm

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.

User avatar
Jack Zhao
Posts: 94
Joined: Thu Jul 24, 2014 7:15 am

Re: Array<string> destructor incurs an exception

Post by Jack Zhao » Fri Feb 23, 2018 10:12 pm

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

User avatar
Thomas Uchida
Posts: 1778
Joined: Wed May 16, 2012 11:40 am

Re: Array<string> destructor incurs an exception

Post by Thomas Uchida » 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.

User avatar
Jack Zhao
Posts: 94
Joined: Thu Jul 24, 2014 7:15 am

Re: Array<string> destructor incurs an exception

Post by Jack Zhao » Thu Jul 19, 2018 9:02 am

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

User avatar
Thomas Uchida
Posts: 1778
Joined: Wed May 16, 2012 11:40 am

Re: Array<string> destructor incurs an exception

Post by Thomas Uchida » Fri Jul 20, 2018 12:16 pm

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.

POST REPLY