Stack smashing protect failure after calling method of class with pointer

robertfent
Posts: 6
Joined: Mon Aug 16, 2021 10:28 am

Stack smashing protect failure after calling method of class with pointer

Postby robertfent » Sun Mar 06, 2022 5:25 pm

Hello,

I am trying to record temperature and humidity data with on ESP32-WROOM-32 with the Sensor DHT22.
Everything works fine until the first loop() is done. After that I get a 'Stack Smashing Protection failure' error and the esp32 reboots.

I am quite new to C++ but I think the problem is the usage of pointers here but I'm not sure tbh.

I tried debugging and found out that it crashes after the first loop because I'm calling:
'std::string temp = Convert(sensorPtr->readTemperature());'

But thats all I know. Can anyone help me? :)

This is the code:
sensor-controller.cpp
  1. #include <sensor-controller.h>
  2. #include <Arduino.h>
  3. #include <sstream>
  4.  
  5. // converts float to string
  6. std::string Convert(float number)
  7. {
  8.     std::ostringstream buff;
  9.     buff << number;
  10.     return buff.str();
  11. }
  12.  
  13. SensorController::SensorController(uint8_t gpio_data_)
  14. {
  15.     gpio_data = gpio_data_;
  16. }
  17.  
  18. DHT SensorController::init()
  19. {
  20.     Serial.println("Starting temperature and humidity sensor...");
  21.     // init dht with proper data port
  22.     DHT sensor(gpio_data, DHT22);
  23.     sensor.begin();
  24.     sensorPtr = &sensor;
  25.     // wait 4 second for booting
  26.     delay(4000);
  27.     Serial.println("Sensor connected!");
  28.     return sensor;
  29. }
  30.  
  31. std::string SensorController::readData()
  32. {
  33.     Serial.println("-----Sensor Controller-----");
  34.     std::string temp = Convert(sensorPtr->readTemperature());
  35.     std::string hum = Convert(sensorPtr->readHumidity());
  36.  
  37.     Serial.print("Temp: ");
  38.     Serial.print(temp.c_str());
  39.     Serial.print("°C / Humidity: ");
  40.     Serial.print(hum.c_str());
  41.     Serial.println("%");
  42.  
  43.     std::string sensorData = "{\"temp\": \"" + temp + "\", \"hum\": \"" + hum + "\"}";
  44.     Serial.println("-------------------------");
  45.     return sensorData;
  46. }
sensor-controler.h
  1. #ifndef temp_controller
  2. #define temp_controller
  3.  
  4. // to make dht work with platform.io
  5. // https://community.platformio.org/t/pio-libdeps-esp32dev-dht-sensor-library-dht-u-h29-fatal-error-adafruit-sensor-h-no-such-file-or-directory/21861/2
  6. #include <Adafruit_Sensor.h>
  7. #include <DHT.h>
  8.  
  9.  
  10. class SensorController
  11. {
  12. public:
  13.     SensorController(const uint8_t gpio_data);
  14.     DHT init();
  15.     std::string readData();
  16.  
  17. private:
  18.     uint8_t gpio_data;
  19.     DHT *sensorPtr;
  20. };
  21.  
  22. #endif
main.cpp
  1. #include <Arduino.h>
  2. #include <dotenv-parser.h>
  3. #include <network-controller.h>
  4. #include <http-controller.h>
  5. #include <mic-controller.h>
  6. #include <sensor-controller.h>
  7. #include <sstream>
  8.  
  9. // mic gpios
  10. const byte gpio_sck = 33;
  11. const byte gpio_ws = 32;
  12. const byte gpio_sd = 34;
  13.  
  14. // using led with g23 and gnd
  15. const byte gpio_led = 23;
  16.  
  17. // sensor gpio
  18. const byte gpio_data = 26;
  19.  
  20. // ip to my rpi in the home network
  21. HttpController httpController("http://{ip}:8080");
  22. SensorController sensorController(gpio_data);
  23.  
  24. void setup()
  25. {
  26.   // init g32 as an output
  27.   pinMode(gpio_led, OUTPUT);
  28.  
  29.   // launch on proper port I guess
  30.   Serial.begin(9600);
  31.  
  32.   // init env vars
  33.   DotenvParser parser;
  34.   parser.parseFileContent();
  35.  
  36.   // init network controller if both vars are set
  37.   if (getenv("SSID") && getenv("PASSWORD"))
  38.   {
  39.     NetworkController networkController(getenv("SSID"), getenv("PASSWORD"));
  40.     networkController.connect();
  41.   }
  42.   else
  43.   {
  44.     Serial.println("Ssid or password not given. Can't connect to network");
  45.   }
  46.  
  47.   sensorController.init();
  48.  
  49.   // MicController micController(gpio_sck, gpio_ws, gpio_sd);
  50.   // uint8_t *record = micController.record();
  51. }
  52.  
  53. void loop()
  54. {
  55.   // power led on
  56.   // Serial.println("Led on!");
  57.   digitalWrite(gpio_led, HIGH); // HIGH -> 3.3v output
  58.  
  59.   // send alive request
  60.   // const char *data = "{\"data\": \"still alive\"}";
  61.   // httpController.postRequest("/esp/", "application/json", data);
  62.  
  63.   std::string sensorData = sensorController.readData();
  64.   // httpController.postRequest("/esp/sensor", "application/json", sensorData.c_str());
  65.  
  66.   // wait 10 sec
  67.   // delay(10000);
  68.   // wait 300 sec = 5min
  69.   sleep(300);
  70.  
  71.   // turn off led
  72.   digitalWrite(gpio_led, LOW); // set voltage level to LOW to power off gpio -> 0v
  73.   // Serial.println("Led off!");
  74.   // wait 1 sec
  75.   delay(1000);
  76. }
This is the error message:
"
[...]
-----Sensor Controller-----
Temp: 21.7°C / Humidity: 39.8%
-------------------------


Stack smashing protect failure!

abort() was called at PC 0x4013860f on core 1

ELF file SHA256: 0000000000000000

Backtrace: 0x400887ac:0x3ffb1f10 0x40088a29:0x3ffb1f30 0x4013860f:0x3ffb1f50 0x400d0b9d:0x3ffb1f70 0x400d6239:0x3ffb1fb0 0x40089a3a:0x3ffb1fd0

Rebooting...
"

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Stack smashing protect failure after calling method of class with pointer

Postby Craige Hales » Mon Mar 07, 2022 12:15 am

maybe https://www.cplusplus.com/reference/cstdlib/getenv/
The pointer returned points to an internal memory block, whose content or validity may be altered by further calls to getenv (but not by other library functions).
Not sure how that plays out when two getenv calls together here

Code: Select all

NetworkController networkController(getenv("SSID"), getenv("PASSWORD"));
try copying them to separate variables before the call.

edit: copy the data, not just the pointers. more info https://github.com/espressif/arduino-esp32/issues/1455
Craige

Who is online

Users browsing this forum: No registered users and 133 guests