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

 
 

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

Submitter:  Pascal Dupuis <cdemills>
Submitted:  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
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

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:

http://hg.savannah.gnu.org/hgweb/octave/rev/0574c36a095e
http://hg.savannah.gnu.org/hgweb/octave/rev/c74ff452e2bb
http://hg.savannah.gnu.org/hgweb/octave/rev/34d06c73b48d

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>
Group 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>
Group 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
  endfunction
  fh = @nested_func;
endfunction


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>
Group administrator
Sat 27 Feb 2021 03:25:02 PM UTC, original submission:  

Hello,
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"

Regards

Pascal

Pascal Dupuis <cdemills>
Group Member

 

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

Attach Files:
   
   
Comment:
   

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
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group 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 -email is unavailable-

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code