bugGNU Octave - Bugs: bug #60137, handles to nested functions do not...


bug #60137: handles to nested functions do not properly capture context

Submitted by:  Pascal Dupuis <cdemills>
Submitted on:  Sat 27 Feb 2021 03:25:02 PM UTC  
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Pascal Dupuis Open/Closed:  Closed
Release:  6.2.0 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.


Thu 01 Apr 2021 07:36:58 AM UTC, comment #3: 

I pushed a series of changes on stable to fix this problem for nested and anonymous functions:


These changesets make changes in public interfaces, which is something that we try to avoid during a stable release series.  OTOH, they are important to make handles to nested and anonymous functions work properly.  And since handles to nested functions were one of the big new features of Octave 6, I'm in favor of making this change in version 6 even if it causes some (small) backward compatibility issues.  These interfaces are not something that typical users (even those who write .oct files) will encounter.

John W. Eaton <jwe>
Project Administrator
Tue 02 Mar 2021 05:39:13 PM UTC, comment #2: 

The attached patch appears to solve the immediate problem for me, but while investigating this bug I realized there is another serious memory management issue with handles to nested functions.  I'll create a separate discussion on discourse and/or a bug report about that.

(file #50975)

John W. Eaton <jwe>
Project Administrator
Tue 02 Mar 2021 02:39:10 AM UTC, comment #1: 

Confirmed.  A simpler example is

function fh = test_nested_func ()
  x = sin (pi);
  y = 13;
  function nested_func ()
    y  %% OK
    x  %% UNDEFINED
  fh = @nested_func;

then I see the following:

octave:1> fh = test_nested_func ()
fh = @nested_func
octave:2> fh ()
y = 13
error: 'x' undefined near line 6, column 6
error: called from
    test_nested_func>nested_func at line 6 column 5

It's quite strange that the value of Y is captured, but not X in this example.

This problem happens with current default, stable, 6.2.0, and 6.1.0.  Since handles to nested functions were introduced in 6.1.0, I'm not sure this can really be considered a regression, but it is something that should work.  I'll look at fixing it for 6.3.0.

John W. Eaton <jwe>
Project Administrator
Sat 27 Feb 2021 03:25:02 PM UTC, original submission:  

As suggested by JWE, here's a report about issues with available variables in nested scope. The idea is to initialize some context with a few constant matrices and some variables.

The expected behavior is to get a new result from a matrix operation after test_nested_script lines 6 and 7, where the increase() and decrease funcs are called.

Under MatLAB R2018b, the test "exist('x', 'var')" succeeds and the size of x is displayed: (2, 1) for ctr1 and (10, 1) for ctr2. Under Octave, the behavior w.r.t. n and n0 is OK, but variables
"A regmat x y" are not available in the subfunctions.

Platform= CentOS 8 stream, gcc-8.4.1, compile options
"--without-qt --without-qscintilla --enable-address-sanitizer-flags --disable-float-truncate --disable-docs"



Pascal Dupuis <cdemills>
Project Member


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

Attach Files:

Attached Files
file #50975:  closure-vars-patch.txt added by jwe (1KiB - text/plain)
file #50935:  test_nested_func.m added by cdemills (1KiB - text/x-objcsrc)
file #50936:  test_nested_script.m added by cdemills (212B - text/x-objcsrc)


Depends on the following items: None found

Items that depend on this one: None found


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

    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 10 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-04-06 rik5 StatusReady For Test => Fixed
        Open/ClosedOpen => Closed
    2021-04-01 jwe StatusConfirmed => Ready For Test
    2021-03-02 jwe SummaryClosures: differences in behavior with MatLAB => handles to nested functions do not properly capture context
    2021-03-02 jwe Attached File- => Added closure-vars-patch.txt, #50975
        Item GroupRegression => Incorrect Result
    2021-03-02 jwe StatusNone => Confirmed
    2021-02-27 cdemills Attached File- => Added test_nested_func.m, #50935
        Attached File- => Added test_nested_script.m, #50936
        Carbon-Copy- => Added cdemills@gmail.com

    Back to the top

    Powered by Savane 3.7