bugGNU Octave - Bugs: bug #64242, destructor is executed even if the...


bug #64242: destructor is executed even if the constructor fails and throws an error

Submitter:  Vipul Cariappa <vipul_cariappa>
Submitted:  Tue 23 May 2023 02:50:31 PM UTC
Category:  Classdef Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Other
Status:  Wont Fix Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * 8.2.0 8.X Series Bug Fix Release Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment (Rich Markup)

Wed 31 May 2023 12:28:44 AM UTC, comment #2: 

We should keep Matlab compatible behavior here.  Also, this is the correct behavior from the point of view of either interpreter.  The interpreter doesn't know where in the constructor an error resulted.  Maybe it was after significant memory had been allocated?  The best response is for the interpreter to hand the partially constructed object over to the destructor and have it do whatever is necessary to clean up the partially or fully constructed object.

Marking as "Won't Fix".

Rik <rik5>
Group administrator
Tue 23 May 2023 04:27:35 PM UTC, comment #1: 

Fwiw, in Matlab R2023a:

>> myclass()
myclass Delete called
Not enough input arguments.

Error in myclass (line 10)
            self.value = x;

So, it also seems to be calling the destructor for the failed constructor. (The output is in a different order though. But I don't think that is very important.)

I'll leave the report open in case others think we should deviate from Matlab's behavior in that case.
But my two cents are for leaving as it is (i.e., compatible).

Markus M├╝tzel <mmuetzel>
Group Member
Tue 23 May 2023 02:50:31 PM UTC, original submission:  

Destructor gets executed, even if the constructor fails and throws an error, in a classdef.

My Code (Also attached):

classdef myclass < handle



        function self = myclass (x)
            self.value = x;
            % stuff related to acquiring memory

        function delete (self)
            fprintf ('myclass Delete called\n');
            % stuff related to freeing memory



octave:1> myclass()
error: 'x' undefined near line 10, column 26
error: called from
    myclass at line 10 column 24
myclass Delete called

The "myclass Delete called" should not have been displayed, because the construction of myclass actually failed.

To me, this looks like an error. I believe that the destructor should not be called if the constructor fails.

Vipul Cariappa <vipul_cariappa>


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

Attach Files:

Attached Files
file #54773:  myclass.m added by vipul_cariappa (395B - text/plain)


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 mmuetzel (Posted a comment)
  • -email is unavailable- added by vipul_cariappa (Submitted the item)

    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 project members can vote.





    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-05-31 rik5 StatusNone Wont Fix
        Open/ClosedOpen Closed
    2023-05-23 vipul_cariappa Attached File- Added myclass.m, #54773

    Back to the top

    Powered by Savane 3.12