Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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
per isakson
per isakson on 9 Oct 2012
Edited: per isakson on 9 Oct 2012
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.

Answers (0)

Community Treasure Hunt

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

Start Hunting!