diff --git a/ScreenUi.cpp b/ScreenUi.cpp index 7ed03d5..fad4e79 100644 --- a/ScreenUi.cpp +++ b/ScreenUi.cpp @@ -124,7 +124,9 @@ Container::Container() { } Container::~Container() { - free(components_); + if (components_) { + free(components_); + } } void Container::update(Screen *screen) { @@ -154,11 +156,7 @@ void Container::repaint() { void Container::add(Component *component, int8_t x, int8_t y) { if (!components_ || componentsLength_ <= componentCount_) { uint8_t newComponentsLength = (componentsLength_ * 2) + 1; - Component** newComponents = (Component**) malloc(newComponentsLength * sizeof(Component*)); - for (int i = 0; i < componentCount_; i++) { - newComponents[i] = components_[i]; - } - free(components_); + Component** newComponents = (Component**) realloc(newComponentsLength * sizeof(Component*)); components_ = newComponents; componentsLength_ = newComponentsLength; } @@ -354,6 +352,10 @@ List::List(uint8_t maxItems) : Label(NULL) { captured_ = false; } +List::~List() { + free(items_); +} + void List::addItem(const char *item) { items_[itemCount_++] = (char*) item; if (text_ == NULL) { diff --git a/ScreenUi.h b/ScreenUi.h index 06a43e9..661910b 100644 --- a/ScreenUi.h +++ b/ScreenUi.h @@ -53,7 +53,7 @@ class CharSet { class RangeCharSet : public CharSet { public: RangeCharSet(int rangeCount, ...); - ~RangeCharSet(); + virtual ~RangeCharSet(); virtual int charAt(int index); virtual unsigned char size(); private: @@ -125,7 +125,7 @@ class Component { class Container : public Component { public: Container(); - ~Container(); + virtual ~Container(); virtual void add(Component *component, int8_t x, int8_t y); virtual void update(Screen *screen); // Paints any dirty child components. @@ -258,6 +258,7 @@ class Checkbox : public Label { class List : public Label { public: List(uint8_t maxItems); + virtual ~List(); void addItem(const char *item); const char *selectedItem() { return items_[selectedIndex_]; } uint8_t selectedIndex() { return selectedIndex_; } @@ -333,7 +334,7 @@ class ScrollContainer : public Container { // better than adding this property to every other object or walking // the tree to find it. ScrollContainer(Screen *screen, uint8_t width, uint8_t height); - ~ScrollContainer(); + virtual ~ScrollContainer(); virtual void paint(Screen *screen); virtual bool dirty(); #ifdef SCREENUI_DEBUG