Is it advisable to have a handle object inside a handle object?

Hi,
I would like to create two classes, say, class1 and class2 both of which inherit from the handle class. Therefore I have
classdef class1 < handle
properties
prop1
end
end
classdef class2 < handle
properties
prop2
end
end
Now suppose I create an instance for each of the classes, say a1=class1 and a2=class2. Is it advisable to set
a1.prop1=a2
a2.prop2=a1
?. That is, I have put each object inside each other. The advantage of this strategy is that I can easily manipulate a2 from within the body of a1 and vice versa. The problem, I don't know whether it is really a problem, is the clear circularity or infinite loop this strategy creates: a1 is inside a2 which is inside a1, inside a2,...
Is this advisable to do this? Is there an alternative way of dealing with this? i.e. being able to manipulate each object from within the body of the other.
Thanks

 Accepted Answer

Such coupling between two classes is usually indication of a flaw in your object model. Sometimes, it is the most efficient solution. What is your use case? Normally, one object is clearly the container for the other.
I'm not sure how matlab memory manager copes with such coupling as Mathworks does not document how it works, so if you do implement this, you need to implement a delete method in both objects that make sure to clear the reference to the other object. Otherwise as you say you have circularity. Perhaps matlab's memory manager can cope with that (it detects that neither object is accessible from the main workspace?), perhaps not.

8 Comments

Guillaume, Thanks for the reply. I have tested it and it seems to work but I do not like it. To be more concrete, one of the objects could be a car and the other object the owner of the car. The owner can own many cars and should have a handle to his cars and each car should be identifiable by its owner. Suppose then that a time-varying insurance has to be paid on each car and how much to pay is decided by a method inside the car object. If the owner is asked to pay the insurance, I would like him to ask the car how much to pay.
I do not know how handle objects work; for regular objects there is reference counting.
Thinking more about it what you describe is actually a common pattern. Microsoft uses it for their Form hierarchy (a Form contains Controls and Containers which may contain more Control|s and |Containers each with a reference to its parent). Similarly matlab uses it for their graphical hierarchy. A Figure has a Children property which contains Axes. The Axes have a Parent property pointing back to the Figure.
So, forget what I said. The pattern is fine. But as said, you ought to make sure that you have a delete method in each class to help in breaking the circular referencing
There is no danger of a memory leak or need for the delete methods to help MATLAB. When there are no external references to the loop of objects then all objects in a loop will be deleted. Just remember that a reference need not be a workspace variable, it could be a function handle in a callback or a value in a graphics or other object.
Thanks Guillaume, Walter and Philip,
I take it then that it is safe to proceed that way, i.e. putting "a1" into "a2" and then "a2" into "a1".
What remains now is suppose I delete a1, what happens to a2.prop2, which originally contained a1?
Is there anyway it can be automatically deleted from within a2.prop2? Philip talks about callback, perhaps that is a solution but I have never used callbacks or events before. Please advise!
Thanks,
If you explicitly delete a1 then a2.prop2 will become a handle to a deleted object which is not usually very desirable unless you have listeners expecting this behaviour and reacting or your code that uses prop2 in a2 expects that it may be in this state and has methods to handle it (still not nice though).
You can use
isvalid( a2.prop2 )
to check if it is a valid object rather than a deleted one, but this is always a tricky part of any object-oriented design when objects have references or pointers to each other and then the object gets deleted from somewhere else that your object knows nothing about.
If your instance a1 simply goes out of scope or, for example you do
clear a1
then the object will still exist as before in a2.
Philip, that implies that MATLAB uses a mark/sweep algorithm along with reference counting. That possibility is something that I have not seen any previous evidence for, and have seen evidence against for regular data. Is the mark/sweep exclusive to handle class?
It certainly would be great if details of the memory management for classes were actually documented (in the doc not on the website or forums) so that those of us that are used to having to deal with this in other languages don't have to wonder how it works in matlab.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!