Callback still running after Quit Debugging

4 次查看(过去 30 天)
Hello all,
I'm using the Add-On "MQTT in Matlab", but probably this is a generic scenario.
In the constuctor of my class (MqttValue), I'm subscribing to a MQTT topic with a normal callback. This works like a charm
function obj = MqttValue(mqttClient, topic)
% MORE code...
% subscribe to topic
obj.mqttSubscription = subscribe(mqttClient.mqttBrokerConnection, topic, 'Callback', @obj.mqttCallback);
% MORE code...
end
function mqttCallback(obj, topic, msg)
% handle mqtt callback, when new/changed value is received for topic
fprintf('\nMqtt callback received:\ntopic: "%s"\nmessage: "%s"\n', topic, msg);
end
When I "Run" the script the MQTT data is comming in and with every publish I get a new message in the console.
Now I hit "Quit Debugging". The MQTT broker is still sending some data to the topics in the background which is fine. But the console is still creating output for that data!?!?
My question is: How do I get the callback(s) to stop receiving data when I hit the "Quit Debugging" button?
When I hit the "Run" button again, I get all data doubled in the console, as the old and the new callback react on the MQTT broker sending data.
Closing and reopening Matlab works, but is really troublesome...
  2 个评论
Michael Van de Graaff
I also have issues with quitting debuggin. It takes several seconds to actually quit debugging, when normally that process is essentially instant. as of now I just wait and twiddle my thumbs
Michael Boegler
Michael Boegler 2022-3-7
I again thought about it and suppose that it would be best to call the unsubscribe function within the destructor of the class to make sure that the subscriptions are canceld when the class is destroyed.
But quit debugging does not call the destructer (delete) function of a class... :(

请先登录,再进行评论。

回答(1 个)

Poorna
Poorna 2024-2-9
Hi Michael,
I see you are trying to exit clean from the program when you quit the program using the “Quit Debugging” button. As you have pointed, there is no callback facility for the “Quit Debugging” button.
To completely stop receiving the messages and stop processing them you need to explicitly call the “unsubscribe” function after you press the “Quit Debugging” function. You can write a small script the unsubscribes from the message input and can run it after quitting and before you rerun the program something like below:
unsubscribe(mqttClient, Topic=topic);
You could also do this inside the destructor of the class. In which case you would have to clear all the workspace variables before you rerun the program as below.
clear(mqttClient);
To know more about the “unsubscribe” function please refer to the following documentation:

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by