0

I need to control destruction of a value-class object in Matlab. The problem is as following.

I have a some program (let's call it MyProg) that during execution creates a value-class object (lets call it MyValClass). MyValClass has a handle-class object as one of its properties (lets call it MyHandClass). That handle class initiates some events that MyProg listens to.

The problem appears is MyValClass object is destroyed (usually it happens on exceptions or user termination). I guess, that because there are still listeners listening to events of MyHandClass, the MyHandClass object is not destroyed, and remains in memory.

I would like to control the destruction of MyValClass object, so that I could implicitly delete its MyHandClass property. Is it possible?

Regolith
  • 2,944
  • 9
  • 33
  • 50
sirUjin
  • 13
  • 1
  • 3
    Hi and welcome to Stack Overflow! It would be most helpful for us to see a [mcve], in order to know what your classes look like and how you destroy them. Could you please [edit] the question to contain some sample code? – Adriaan Aug 26 '19 at 08:05
  • 2
    You can just add a `delete` method to your class, which will automatically be called as part of the cleanup. [Docs reference](https://uk.mathworks.com/help/matlab/matlab_oop/handle-class-destructors.html). Within this method, you can delete associated objects if you need to. – Wolfie Aug 26 '19 at 09:51

2 Answers2

1

Here is a bit of background on handle vs value classes:

  1. Objects of a value class are not "destroyed", and they do not have a destructor method. Think of value classes as behaving like the variable a when you've set a = 1. a is not "destroyed" when you type clear a, there's just no variable a any more. Value objects are just data, like 1, and they don't get destroyed.

  2. The above is true even if the value class has a method called delete. A delete method on a value class is just like any other method. It is not a destructor, and it does not get automatically called when the variable is cleared. It only gets called when you explicitly call it.

  3. Handle classes always, whether you implement one or not, have a method called delete, which is a destructor method (i.e. is called when the object is destroyed). If you don't implement one, they will be given a default method called delete, which does nothing other than destroy the object. If you implement a delete method, MATLAB will run that when destroying the object. But there is always a delete method that is a destructor, even if you don't implement one.

So - to your question - if you wish to control the destruction of MyValClass, you must change it to become a handle class. If it's a value class, it is not destroyed and there's nothing to control.

There are other things you might be able to do instead of directly "controlling the destruction". For example, you create an onCleanup object. This is a class that does nothing but execute a user-specified function on its destruction (it's a handle class, so it can do this). So if your code exits because of an exception or user termination, the onCleanup destructor will execute. You could, for example, put some code in there that would explicitly find references to MyHandlClass and deleted them.

Hope that helps!

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
0

The destructor for value object may be necessary, for example, if the class is responsible for accessing data from file and you want to close file when your accessor goes out of scope. There are number of reasons why you would not want such class to be a handle class (e.g. weird behavior on array of objects)

Other reason is creating existing objects counter.

My solution is to define hidden property, delete_, and assign onCleanup(@()something) object to this property. The function, provided to onCleanup would close the files, decrement object counter etc....

  • What if the value object is copied? When the first copy is destroyed, your `onCleanup` will be called, and the other copies of the object will not have access to the resources. If the object owns resources, it must be a handle object, if it doesn’t own resources it should be a value object. This is the only distinction between the two types. – Cris Luengo Oct 25 '21 at 18:18
  • Yes, this is correct. `a=b` for value classes does not work in Matlab as it does not allow to overload `=` operator. We had to write custom `copy` operator and inform users that using `a=b` may bring unexpected consequences. It is still better then using handle classes though we have not fully explored the possibilities of `mixing.Copyable`. May be moving to handle classes will be less hustle after all. – user1077392 Nov 02 '21 at 09:32