octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #49010] mxSetDimensions for cell arrays


From: Rik
Subject: [Octave-bug-tracker] [bug #49010] mxSetDimensions for cell arrays
Date: Thu, 8 Sep 2016 18:54:47 +0000 (UTC)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0

Follow-up Comment #1, bug #49010 (project octave):

It may work in Matlab, but the documentation seems to suggest that anything in
the prhs array is supposed to be constant and left unchanged.  Changing the
dimensions of a supposedly const mxArray is probably not good practice.

>From the documentation
(http://www.mathworks.com/help/matlab/matlab_external/matlab-data.html),


Input Argument prhs

An mxArray passed to a MEX-file through the prhs input parameter exists
outside the scope of the MEX-file. Do not free memory for any mxArray in the
prhs parameter. Additionally, prhs variables are read-only; do not modify them
in your MEX-file.


It seems that the correct approach is to duplicate any input array that you
may modify.  A note on memory management
(http://www.mathworks.com/help/matlab/matlab_external/memory-management-issues.html#f24829)
talks about cell arrays


Incorrectly Constructing a Cell or Structure mxArray

Do not call mxSetCell or mxSetField variants with prhs[] as the member array.
Example

In the following example, when the MEX file returns, MATLAB destroys the
entire cell array. Since this includes the members of the cell, this
implicitly destroys the MEX file's input arguments. This can cause several
strange results, generally having to do with the corruption of the caller's
workspace, if the right-hand side argument used is a temporary array (for
example, a literal or the result of an expression):

myfunction('hello')
/* myfunction is the name of your MEX file and your code
/* contains the following:    */

    mxArray *temp = mxCreateCellMatrix(1,1);
      ...
    mxSetCell(temp, 0, prhs[0]);  /* INCORRECT */

Solution

Make a copy of the right-hand side argument with mxDuplicateArray and use that
copy as the argument to mxSetCell (or mxSetField variants). For example:

mxSetCell(temp, 0, mxDuplicateArray(prhs[0]));  /* CORRECT */



    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?49010>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]