# OOP destructor without code

**URL:** <https://forum.arduino.cc/t/oop-destructor-without-code/1385784>\
**Category:** Programming\
**Created:** [May 30, 2025, 9:23pm UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784 "2025-05-30T21:23:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [May 30, 2025, 9:23pm UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784/1 "2025-05-30T21:23:34Z")

</div>

Hi,  
I have a general question to destructors in programs on arduino controllers where code is in flash memory.

I have a class the constructor of which is instantiating further classes like buttons.  
Is it necessary for the destructor of this class to contain code to delete the button instances or is it sufficient just to declare the destructor without further code?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 30, 2025, 9:52pm UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784/2 "2025-05-30T21:52:48Z")

</div>

> [@SupArdu](#):
>
> Is it necessary for the destructor of this class to contain code to delete the button instances or is it sufficient just to declare the destructor without further code?

Will need more details and example code. But, generally, it depends if you're instantiating the objects dynamically. If so, you need to `delete` them in your destructor or use features like smart pointers (eg [std::unique\_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr.html)) that do so automatically.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [May 31, 2025, 9:57am UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784/3 "2025-05-31T09:57:02Z")

</div>

It's best to avoid destroying objects at all on most types of Arduino. Most Arduino have no "garbage collection" process, so creating & destroying objects dynamically will probably result in a fragmented heap, and, not long after, running out of memory. When memory is all used up, there is no error message, your code tries to continue, but weird things start to happen because memory that is in use starts to get overwritten.

Using memory dynamically is a useful technique on PCs/laptops/servers because there are always multiple processes running which can take advantage of memory that gets freed up by other processes that are no longer using it. (And there are garbage collection process that prevent heap fragmentation.)

But on most Arduino and other microcontrollers, there is only ever one process running, so the only reason to destroy objects and free up dynamic memory is when that single process can re-use the memory for other purposes. That is a pretty rare thing in microcontroller applications.

So my answer to your question is no, don't worry about it. And avoid creating objects dynamically. Create them statically, as many as you need or as many as will fit into the Arduino's limited memory. That way, you will know at compile time if the Arduino has enough memory to run your code reliably. If not, find ways to reduce the memory required or upgrade to an Arduino with more memory.

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [June 2, 2025, 8:33am UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784/4 "2025-06-02T08:33:05Z")

</div>

> [@SupArdu](#):
>
> I have a class the constructor of which is instantiating further classes like buttons.  
> Is it necessary for the destructor of this class to contain code to delete the button instances or is it sufficient just to declare the destructor without further code?

It all depends on how furherButtons get constructed. If you create furtherButton instance in the heap memory like 'furtherButton \*bp = new furtherButton' then you should also care destroying it yourself. If you declare it in the stack, like 'furtherButton b;' the destructor will be called automatically by the code generated by C++ compiler.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 29, 2025, 8:34am UTC](https://forum.arduino.cc/t/oop-destructor-without-code/1385784/5 "2025-11-29T08:34:01Z")

</div>

Closed on November 29, 2025, 8:34am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
