MATLAB: How to avoid: “… . Cannot clear this class or any of its super-classes.”

classclear classeshandle classobject lifecycleoop

I have a GUI, MyGui, which contains a dozen uicontrols. There is nothing fancy: no timers, no listeners, … . The uicontrols are wrapped up in user defined classes in a hierarchy. (It's about reuse of functional groups of uicontrols.) MyGui doesn't include any special code to handle the close down process. Handle graphic handles are values of user class properties.
The following steps illustrates the problem
  • restart Matlab
  • start MyGui
  • close MyGui by using the red close-button in the upper right corner
So far everything seems to be ok. Next, I try to clear the classes, which wraps the uicontrols:
>> clear all, clear classes
which produces a dozen warnings, one for each wrapper
Warning: Objects of 'ToggleButton' class exist. Cannot clear this class or any
of its super-classes.
.....
To cleanup, I need to restart Matlab. There must be a better way!
In search for a solutions I
  • read the entries in the documentation on "Object Lifecycle".
  • experiment with the FEX-contribution ScrollPanel by Jason Kinchen
The documentation make me believe that Matlab is intended to handle the cleanup automatically. ScrollPanel doesn't exhibit the problem and I cannot find any special code to ensure that the instance of ScrollPanel is deleted. There is code to ensure that handle graphic objects are deleted if ScrollPanel is deleted.
I have search www.mathworks.com for "Cannot clear this class" more than once without finding anything except for issues in rather special circumstances.
So what should I do next?

Best Answer

Create a reproducible example and submit a bug report. The problem has been noticed in a number of different scenarios. Perhaps you came across these
All of them were found to be bugs.
If you're sure you've been very careful not to leave other figure windows open which shared appdata or other things with MyGUI, and you've been very careful not to create nested handle objects or handle objects that point to themselves, my tendency is to assume that it's a bug.