How to find listeners attached to an object
이전 댓글 표시
Hi,
I thought this would be a trivial task, but I cannot find the way to do this. Given an object in your workspace, is there an easy way to get handles to all the listeners that have been defined on this object?
cheers, Daniel
댓글 수: 5
Joakim Magnusson
2014년 8월 14일
Why do you want to get handles to all listeners for an object?
Adam Danz
2019년 11월 15일
Related: how to determine if event has a listener
Guillaume
2019년 11월 15일
This reminds me that the other day I was playing around with meta.class and its two events InstanceCreated and InstanceDestroyed and attaching listeners without keeping the listener handles around. I thought I'd just delete the meta.class object when I'm done and that'll destroy the listeners.
Oops! meta.class objects are singleton (undocumented!) and kept around even if you delete your instance (undocumented!) so here I was with lots of listeners firing, no way to list the listeners (as per this thread) and no way to destroy them either,
clear classes is the only way to get rid of meta.class objects once they've been created.
Adam
2019년 11월 15일
From a code design perspective the original question does seem to head down a route that is not generally advisable. An object that allows listeners to be attached should not ever need to know what those listeners are. The example Adam Danz gave is understandable, to know if there are listeners at all, but an object knowing actually what is listening to it seems like a case of knowing far too much.
It would be akin to a radio host being able to find out the names of everyone listening to his show!
That's not to say I haven't also fallen into numerous traps with listeners firing and giving me pages of errors for objects that no longer exist, but whose listener still exists, etc, etc, but it should never be up to the object being listened to to directly interact with those doing the listening. Otherwise some more concrete form of two-way object interaction should be used I would think, as listeners are inherently a one-way communication.
I wish there were a way to h=findall('listeners') or h=findall(obj,'listeners') or something like that because some listeners are created by functions that do not return object handles and I don't know of any other way to get the listener handles (although I've only searched briefly).
[addendum]
To add context to my previous comment that brought this 5+ year thread back to life, I am (was; partially gave up) trying to troubleshoot this problem:
답변 (2개)
Harry Dymond
2020년 7월 3일
4 개 추천
Found the answer on StackOverflow: any object with listeners attached will have an undocumented property .AutoListeners__, which is a cell array of listener handles. The cell array does not appear to get "pruned" if listeners are deleted, so .AutoListeners__{x} could contain a handle to a deleted listener. If the object has never had listeners created for any of its properties, .AutoListeners__ will not exist.
Joakim Magnusson
2014년 8월 14일
Can't you save your listeners when created like this? :
handles.listener1 = addlistener(...);
handles.listener2 = addlistener(...);
etc...
Or that wont work for you?
댓글 수: 2
Daniel
2014년 8월 14일
Joakim Magnusson
2014년 8월 14일
No i couldn't find anything like that either.
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!