How to avoid: "... . Cannot clear this class or any of its super-classes."

37 views (last 30 days)
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?
  2 Comments
Matt J
Matt J on 17 Dec 2012
MyGui doesn't include any special code to handle the close down process. Handle graphic handles are values of user class properties.
You might want to clarify this part. If you have user-defined handle classes, you should be implementing delete() methods for those classes.
per isakson
per isakson on 17 Dec 2012
Edited: per isakson on 17 Dec 2012
My classes, which inherits from handle, do not implement delete() methods. I see no need in this case. The doc says:
Use a class destructor [i.e. delete()] to perform any necessary cleanup operations before MATLAB destroys an object of the class.
and as an example doc describes closing of files. In this case, I see no cleanup task to perform in delete().

Sign in to comment.

Accepted Answer

Matt J
Matt J on 17 Dec 2012
Edited: Matt J on 17 Dec 2012
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.
  2 Comments
per isakson
per isakson on 17 Dec 2012
Thanks for the reminder!
This answer by Malcolm Lidierth to Unable to clear classes looks like a recipe to solve my problem
Object references can leak via userdata or appdata, callbacks, the variable editor etc. Try closing all figures - but probably close the variable editor first.
With handle classes I have taken to maintaing a list in the figure appdata area maintained by calling a common superclass method from the constructors, and adding a DeleteFcn callback that calls a static superclass method to delete any remaining valid handle objects when a figure closes. A bit of a pain but it seems to work.
per isakson
per isakson on 17 Dec 2012
Now I have implemented the scheme of Malcolm Lidierth and so far it seems to work.
However, I still think that the Matlab documentation did promise to solve this automatically.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!