1

I created a class for TFT buttons and messages when using a Wemos 2.4 tft touch display.

In a code I use about 12 buttons (3 different screens with 4 buttons in each screen ), for example in my home control code "Main Screen" has Lights, Windows Alarms, Temperatures, and each screen below has set of others.

Since button's life expectancy is only for current screen (also in loop() since it monitor press at defined location), and each button take no little resources as show below in lib's .h file (other class with touch function are not show here), I would like to know how to create a destructor to free up space, since main resources are used in MessageTFT class.

#ifndef TFT_GUI_h
#define TFT_GUI_h

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>

extern XPT2046_Touchscreen ts; /* Touch screen */
extern Adafruit_ILI9341 tft;   /* Graphics */

const uint8_t _pos_corr_factor[3] = {3, 6, 9}; /* Center text inside a box */
class MessageTFT
{
public:
  uint8_t a, b;
  int xc, yc;
  uint8_t txt_size = 1;
  uint8_t screen_rotation = 0;
  uint8_t border_thickness = 1;
  uint16_t face_color = ILI9341_GREEN;
  uint16_t border_color = ILI9341_RED;
  uint16_t txt_color = ILI9341_BLACK;
  
  char txt_buf[30];
  bool roundRect = false;

public:
  MessageTFT(Adafruit_ILI9341 &_tft);
  void drawMSG();
  void text(char *txt);
private:
  uint8_t _radius = 15;

private:
  void _drawFace();
  void _drawBorder();
  void _put_text();
};
#endif
1
  • 2
    I don't see anything in the button that needs cleanup on destruction. Destructors are used to clean up an instance's dynamic resources (like malloc-ed memory). Sep 30 '21 at 11:28
6

Destructors, despite their name, are not used to destroy the object. They are, instead, a "hook" that is called when the object is destroyed. Much like the "constructor" is called when the object is created - it doesn't do the creating.

A destructor is intended to "clean up" an object before it is deleted. That usually entails freeing any memory allocated at runtime (through malloc(), new, etc) by the object.

Since you don't do anything at runtime that needs to be cleaned up there is nothing for your destructor to do. All your variables are simple statically allocated memory as part of the object istelf, so those automatically get destroyed when the object is destroyed.

You can think of it as crushing a car. The seats, engine and steering wheel all get crushed without you having to worry about it. But first you want to take the dog out of the boot - you really don't want him in there when the car gets crushed, it'll get really really messy and the dog will be lost forever ... :(

7
  • 1
    Why is there a dog in the trunk of the car Sep 30 '21 at 14:53
  • @DaveNewton So it doesn't interfere with you while you're driving. It's an estate, not a sedan.
    – Majenko
    Sep 30 '21 at 14:54
  • 1
    lol-whew Just wanted to make sure the doggie was ok ;) Sep 30 '21 at 14:58
  • 1
    @DaveNewton As long as you take it out of the car before crushing it, yes.
    – Majenko
    Sep 30 '21 at 14:59
  • 1
    @Guy.D If you created it as a local variable (not using new and not having it static or in the global scope) then it gets destroyed when it goes out of scope. If you made it with new then you can delete it any time. If it's in the global scope then it's there forever.
    – Majenko
    Sep 30 '21 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.