More oop weirdness - Cannot clear class
显示 更早的评论
R2013a doesn't display this problem.
.
Original question:
The issue, oop weirdness, reported by Matt J in the Newsgroup a couple of years ago seems to be fixed. It's good news that I fail to reproduce it with R2012a.
Now, I think I found a new testcase for The Mathworks testsuite. It is similar to the case reported by Jim Hokanson in Unable to clear classes. (Comment: Jim Hokanson on 27 Nov 2012 at 18:14)
First, restart Matlab (R2012a), then run the following commands and it is time to restart Matlab again.
>> c3m = C3Main( 1 );
>> clear all, clear classes
>> c3m = C3Main( 4 );
>> clear all, clear classes
Warning: Objects of 'C3Main' class exist. Cannot ...
Warning: Objects of 'C3Sub' class exist. Cannot ...
where
classdef C3Main < C3SuperC & C3SuperW
methods
function this = C3Main( N )
if nargin == 0, return, end
c3s = C3Sub( N );
this.addChild( c3s )
end
end
end
classdef C3Sub < handle
properties
Name = 'C3Sub';
val
Parent
end
methods
function this = C3Sub( N )
if nargin == 0, return, end
this( 1, N ) = C3Sub();
for ii = 1 : N
this( 1, ii ).val = 17 * ii;
end
end
end
end
classdef C3SuperC < handle
properties
Children = cell(0);
end
methods
function addChild( this, varargin )
for ca = varargin
child_array = ca{:};
for chld = child_array
this.Children = cat( 2, this.Children, {chld} );
chld.Parent = this;
end
end
end
end
end
and
classdef C3SuperW < hgsetget
properties
Name = 'C3SuperW';
end
end
.
The lines
this( 1, N ) = C3Sub();
for ii = 1 : N
this( 1, ii ).val = 17 * ii;
end
in the class C3Sub seems to be the problem - when N = 4. "This only occurs when there are more than 2 children." (Jim Hokanson). Indeed
>> crm = C3Main( 2 );
>> clear all, clear classes
>> crm = C3Main( 3 );
>> clear all, clear classes
Warning: Objects of 'C3Main' class exist. Cannot clear this ...
Warning: Objects of 'C3Sub' class exist. Cannot clear this class ...
回答(1 个)
per isakson
2012-12-19
编辑:per isakson
2012-12-19
2 个评论
Matt J
2012-12-19
function c3handle_lost()
c3m = C3Main( 4 );
clear c3m
end
or
function c3handle_lost()
c3m = C3Main( 4 );
c3cleanup;
end
per isakson
2012-12-21
编辑:per isakson
2012-12-22
类别
在 帮助中心 和 File Exchange 中查找有关 Handle Classes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!