gysmo38 / mitsubishi2MQTT

Mitsubishi to MQTT with ESP8266 module
GNU Lesser General Public License v2.1
371 stars 132 forks source link

fix String returned from server::arg() dropping out of scope #253

Open louissobel opened 5 months ago

louissobel commented 5 months ago

Problem

the String returned by the server::arg methods (like server::arg("POWER")) drops out of scope right after its c_str() method is called.

I'm using a ESP32. That implementation (https://github.com/espressif/arduino-esp32/blob/2.0.14/libraries/WebServer/src/WebServer.cpp#L549) returns a String by value, so once the String returned by arg() is out of scope, the underlying c_str() pointer becomes invalid.

Without this fix, this was causing the web interface to not take updates — by the time the HeatPump library tried to read the various argument strings they were reset.

Fix

I'm not sure that this is a good fix, but it does work: keeping the returned String in scope until after the setSettings call ensures that the underlying char * buffers aren't cleared.

Maybe a better fix would be to update the settings interface to support a String optionally somehow?

andrewleech commented 4 months ago

Oh, I missed your PR here and added a similar fix into my branch just today if you'd like to compare ideas! https://github.com/gysmo38/mitsubishi2MQTT/pull/248