You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello all,
"process_rpc_message" is calling the callback function "processTemperatureChange". The callback function must return RPC_Response object.
Now lets consider the example [0010-esp8266_esp32_rpc].
İn 0010-esp8266_esp32_rpc.ino file, function loop() we are registering the callback functions:
const std::array<RPC_Callback, 2U> callbacks = {
RPC_Callback{ RPC_TEMPERATURE_METHOD, processTemperatureChange },
RPC_Callback{ RPC_SWITCH_METHOD, processSwitchChange }
};
// Perform a subscription. All consequent data processing will happen in// processTemperatureChange() and processSwitchChange() functions,// as denoted by callbacks array.if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Failed to subscribe for RPC"));
#else
Serial.println("Failed to subscribe for RPC");
#endifreturn;
}
and one of the callback function is :
RPC_Response processTemperatureChange(const RPC_Data &data) {
#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Received the set temperature RPC method"));
#else
Serial.println("Received the set temperature RPC method");
#endif// Process dataconstfloat example_temperature = data[RPC_TEMPERATURE_KEY];
#if THINGSBOARD_ENABLE_PROGMEM
Serial.print(F("Example temperature: "));
#else
Serial.print("Example temperature: ");
#endif
Serial.println(example_temperature);
// Just an response example
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
doc[RPC_RESPONSE_KEY] = 42;
returnRPC_Response(doc);
}
In the callback function 'processTemperatureChange', The 'StaticJsonDocument' object is created locally and passed as an argument to RPC_Response constructor. The called RPC_Response constructor is the one with the parameter JsonVariant.
explicitRPC_Response(JsonVariant variant);
Now the local 'StaticJsonDocument' object is impilicitly converted to 'JsonVariant', which means 'JsonVariant' is referencing the local 'StaticJsonDocument' object.
The 'process_rpc_message' gets the callback function's("processTemperatureChange") return value as a response.
As soon as callback function is finished, local object 'StaticJsonDocument' lifetime is over and the returned value RPC_Response is now referencing a released source 'StaticJsonDocument'. Undefined behaviour.
The solution can be:
RPC_Response class can store the JsonDocument so that we can move the locally created 'StaticJsonDocument' object and then use it when sending the response to the server.
Thanks in advance.
The text was updated successfully, but these errors were encountered:
Hello all,
"process_rpc_message" is calling the callback function "processTemperatureChange". The callback function must return RPC_Response object.
Now lets consider the example [0010-esp8266_esp32_rpc].
İn 0010-esp8266_esp32_rpc.ino file, function loop() we are registering the callback functions:
and one of the callback function is :
In the callback function 'processTemperatureChange', The 'StaticJsonDocument' object is created locally and passed as an argument to RPC_Response constructor. The called RPC_Response constructor is the one with the parameter JsonVariant.
Now the local 'StaticJsonDocument' object is impilicitly converted to 'JsonVariant', which means 'JsonVariant' is referencing the local 'StaticJsonDocument' object.
The 'process_rpc_message' gets the callback function's("processTemperatureChange") return value as a response.
As soon as callback function is finished, local object 'StaticJsonDocument' lifetime is over and the returned value RPC_Response is now referencing a released source 'StaticJsonDocument'. Undefined behaviour.
The solution can be:
RPC_Response class can store the JsonDocument so that we can move the locally created 'StaticJsonDocument' object and then use it when sending the response to the server.
Thanks in advance.
The text was updated successfully, but these errors were encountered: