Some Matlab versions crash when using listener
Mostrar comentarios más antiguos
Dear Forum,
I've been playing around with listeners in Matlab, i.e. implementation of a weak object reference by using listeners. It appears that my code works fine when using release 2016a or 2015b. It, however, crashes on 2014a, 2015a and 2015a/sp. (Technically it is really not crash but rather a freeze, which does not make a difference to me, since I have to kill the program and restart).
I consider that my implementation is an unfortunate pick and by accident it works with the latest releases of Matlab. I also consider that there is bug. In both cases I kindly ask for a workaround in order to enable my program for any Matlab version.
My code looks like this:
First, the object managed by the weak reference:
classdef ManagedObject < handle
%MANAGEDOBJECT Summary of this class goes here
% Detailed explanation goes here
properties
listenToRuntimeQuery;
end
methods
function setupConnection(obj, weakReference)
% setup listener
for i=1:length(weakReference)
% get weak reference
wRef = weakReference(i);
% get instance of object
cObj = obj(i);
callback = @cObj.getObject;
% create listener connection
obj(i).listenToRuntimeQuery = event.listener(weakReference(i), 'notification',...
@(src, evnt) WeakReference.respond(src, evnt, callback));
end
end
function delete(obj)
% delete listener
delete(obj.listenToRuntimeQuery);
% notify
WeakReference.notifyOnDelete();
end
function obj = getObject(obj)
% Helper callback to request object from weak reference
end
end
end
Second, the implmentation of the weak reference:
classdef WeakReference < handle
%SMARTPOINTER Summary of this class goes here
% Detailed explanation goes here
events
notification;
end
properties
reference;
end
% Constructor
methods
function obj = WeakReference()
end
end
methods
function queryRuntime(obj)
% notify
notify(obj, 'notification');
end
end
methods(Static)
function respond(src, evnt, subject)
%disp('responding');
% store reference temporarily
src.reference = subject();
end
end
methods
function reference = getReference(obj)
% query runtime
obj.queryRuntime();
% return reference
reference = obj.reference();
% clear buffer
obj.reference = [];
end
end
methods (Static)
function notifyOnDelete()
disp('Object has been cleared');
end
end
end
Third, my sample script which will hang on second execution at least in 2014a, 2015a and 2015a/sp1.
% clear all
clear classes
% test settings
N = 10000;
% clear test objects
clear('weakRef')
clear('testObject')
% create object array
testObject(N) = ManagedObject();
% create weak listener
weakRef(N) = WeakReference();
% create smartpointer
testObject.setupConnection(weakRef);
% get reference
for i=1:N
weakRef(i).getReference();
end
Can someone help?
Thanks, Daniel
Respuesta aceptada
Más respuestas (1)
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!