bugGNU Octave - Bugs: bug #49010, mxSetDimensions for cell arrays


bug #49010: mxSetDimensions for cell arrays

Submitted by:  Guillaume <gyom>
Submitted on:  Mon 05 Sep 2016 04:20:58 PM UTC  
Category:  Libraries Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Segfault, Bus Error, etc.
Status:  Fixed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  Closed
Release:  dev Operating System:  GNU/Linux

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Fri 09 Sep 2016 02:29:18 PM UTC, comment #4:

@Rik: Many thanks for this, this is very useful. If it can make it to 4.2, it would be great.

You were entirely right in comment 1: my snippet was just a way to reproduce the issue taken from a larger piece of code and I didn't realize I was introducing another subtlety.

Guillaume <gyom>
Thu 08 Sep 2016 09:48:13 PM UTC, comment #3:

Octave was not allocating new memory on the heap for the dims array when mxSetDimensions was called. I fixed that in this cset (http://hg.savannah.gnu.org/hgweb/octave/rev/e337b8e3592c). Both the original example, and my code which makes a duplicate of the inputs before modifying them, now pass. Marking as fixed and closing report.

Rik <rik5>
Project Administrator
Thu 08 Sep 2016 07:48:41 PM UTC, comment #2:

I duplicated the array so that I am not working on the constant inputs. I think this is the correct strategy, but it does still fail which means there likely is a problem with mxSetDimensions in Octave.

Rik <rik5>
Project Administrator
Thu 08 Sep 2016 06:54:47 PM UTC, comment #1:

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),

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

Rik <rik5>
Project Administrator
Mon 05 Sep 2016 04:20:58 PM UTC, original submission:

There seems to be an issue if the new dimensions array passed to mxSetDimensions is freed explicitly when called on a cell array. The following piece of code illustrates the problem by trying to reshape an array with two elements in a 1x2 array:

The segfault disappears if the line "mxFree(dim);" is removed. It works as expected in MATLAB. I assume it is valid to free the dimensions array as mxSetDimensions makes a copy of it, if I understand the documentation right:

"mxSetDimensions allocates heap space to hold the input size array."

It works for numeric and struct arrays, but not cell arrays.

Guillaume <gyom>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by gyom (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-09-08 rik5 StatusNone => Fixed
        Open/ClosedOpen => Closed

    Back to the top

    Powered by Savane 3.4