cannot update class definition in Matlab

巧了我就是萌 提交于 2019-11-30 21:55:36

You have an intermediate instance myClass.father that is not being destroyed by MATLAB. You have to deleteit yourself

>> clear grandpa
>> delete(son.precursors{1})
>> clear son
>> clear classes
>> son = myClass.son
son = 
    son    
>> sumVal(son)
ans =
     6

Edit: Alternatively, you can add a destructor to your class

    function delete(obj)
        if isa(obj.precursors{1}, 'myClass')
            delete(obj.precursors{1});
        end
    end

and use delete(son) instead of leaving it to clear function to destroy. You can extend this to your case and recursively delete all instances in your tree.

Those instances are "hiding" in the myClass enumeration class itself. Matlab is storing a reference to each of those named instances so when you reference them like myClass.father you get the same object back, instead of it constructing a new one. Probably similar to how values are stored in Constant properties on classes.

If you have any other classes that refer to the myClass.xxx enumerated instances in Constant properties, enumerations, or persistent variables, they could also be holding on to references to them.

Try doing clear classes a few times in a row instead of just once.

To help debug this, you could put a couple debugging printf() statements in the constructor and destructor for this class, so you can see when the instances are really created and cleaned up.

  function obj = myClass(pre, num, val)
      % constructor
      if nargin > 0
          obj.precursors = pre;
          obj.numPre = num;
          obj.value = val;
      end
      printf('myClass: created (%d, %d, nargin=%d)\n', obj.numPre, obj.value, nargin);
  end
  function delete(obj)
      printf('myClass: deleting (%d, %d)\n', obj.numPre, obj.value);
  end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!