Problem with handle class destruction when objects reference each other in field of struct

The below example may be more complicated than necessary, but is the simplest thing I could find that replicates the behavior I'm seeing. I have three classes:
classdef MyClassA < handle
properties
x
y
end
methods
function this = MyClassA()
this.x = MyClassD( 'a' );
this.y = MyClassD( 'b' );
end
end
end
classdef MyClassB < handle
properties
myStruct
end
methods
function setMyStruct(this, value)
this.myStruct = [this.myStruct, value];
end
end
end
classdef MyClassD < handle
properties
data
reference1
reference2
end
methods
function this = MyClassD( value )
this.data = value;
end
end
end
Script:
mcb = MyClassB;
mca = MyClassA;
% mca.x.reference1 = mcb;
mca.y.reference1 = mcb;
mca.x.reference2 = mca.y;
mca.y.reference2 = mca.x;
s.f1 = mca.x;
mcb.setMyStruct( s )
s2.f1 = mca.y;
mcb.setMyStruct( s2 )
clear classes
When I run the above script I get a warning "Warning: Objects of 'MyClassD' class exist. Cannot clear this class or any of its super-classes." However I don't get this warning if I remove any of the following from the script:
1) mca.y.reference1 = mcb;
2) mca.x.reference2 = mca.y; mca.y.reference2 = mca.x;
3) s2.f1 = mca.y; mcb.setMyStruct( s2 )
This behavior seems very odd to me. Even stranger, I don't get any warning message if I put an empty delete method (destructor) in either MyClassB or MyClassD (in the form below).
function delete(this)
end
I'm using Matlab 2012a WinXP. Can others replicate this behavior and better yet explain it.

7 Comments

Nor can I in R2012b on WinXP. Though nor can I find a bug in the Matlab bug report that was fixed in 2012b relating to this either...
Yes, can reproduce it without fail in R2012a. I've restarted Matlab and my PC and am able to reproduce it in all cases in R2012a.
Also get the same error in R2012a on Mac OS X
Your problem resembles
Why am I unable to clear class definitions if one of the classes
contains a property initialized to a class having an anonymous
function as its property in MATLAB 7.11 (R2010b)?
Date Last Modified: Friday, October 22, 2010
Solution ID: 1-DIYLPG
Product: MATLAB
Reported in Release: R2010b
There are a few old bugs related to "Cannot clear this class or any of its super-classes". They are reported as fixed. However, I'm convinced there are more to be reported.
Yes this seems to be one that went unreported and quietly fixed in R2012b.

Answers (0)

This question is closed.

Asked:

on 9 Oct 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!