MATLAB Answers

per isakson

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

Asked by per isakson
on 17 Dec 2012
Accepted Answer by Matt J

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 for "Cannot clear this class" more than once without finding anything except for issues in rather special circumstances.

So what should I do next?


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.

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().


No products are associated with this question.

1 Answer

Answer by Matt J
on 17 Dec 2012
Edited by Matt J
on 17 Dec 2012
 Accepted 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.


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.

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.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!