discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

QT classes all have destructors - should we be using them?

V
vulcan_@mac.com
Wed, Sep 24, 2025 3:02 PM

in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like

~QtFileInfo and ~QtSaveFile

Back when i was writing C++ code for a living we tried to be really careful to always invoke the destructors before returning from a function.

Do i get to ignore this in modern day C++ coding practice?

in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like \~QtFileInfo and \~QtSaveFile Back when i was writing C++ code for a living we tried to be really careful to always invoke the destructors before returning from a function. Do i get to ignore this in modern day C++ coding practice?
MK
Marius Kintel
Wed, Sep 24, 2025 3:15 PM

On Sep 24, 2025, at 11:02, vulcan_--- via Discuss discuss@lists.openscad.org wrote:

in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like

~QtFileInfo and ~QtSaveFile

Back when i was writing C++ code for a living we tried to be really careful to always invoke the destructors before returning from a function.

Do i get to ignore this in modern day C++ coding practice?

Can you give a more concrete example?
In general: If you call “new”, you should call “delete” as well, but most of the time you should use smart pointers.
When using Qt, especially when using widgets and other QObject subtypes, some of the memory management is done for you: https://doc.qt.io/qt-6/objecttrees.html

-Marius

On Sep 24, 2025, at 11:02, vulcan_--- via Discuss <discuss@lists.openscad.org> wrote: > > in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like > > ~QtFileInfo and ~QtSaveFile > > Back when i was writing C++ code for a living we tried to be really careful to always invoke the destructors before returning from a function. > > Do i get to ignore this in modern day C++ coding practice? > Can you give a more concrete example? In general: If you call “new”, you should call “delete” as well, but most of the time you should use smart pointers. When using Qt, especially when using widgets and other QObject subtypes, some of the memory management is done for you: https://doc.qt.io/qt-6/objecttrees.html -Marius
JB
Jordan Brown
Wed, Sep 24, 2025 5:58 PM

On 9/24/2025 8:15 AM, Marius Kintel via Discuss wrote:

On Sep 24, 2025, at 11:02, vulcan_--- via Discuss
discuss@lists.openscad.org wrote:

in working with classes like QtFileInfo, QtSaveFile and the like i
see that they all have destructors like

~QtFileInfo and ~QtSaveFile

Back when i was writing C++ code for a living we tried to be really
careful to always invoke the destructors before returning from a
function.

Do i get to ignore this in modern day C++ coding practice?

Can you give a more concrete example?
In general: If you call “new”, you should call “delete” as well, but
most of the time you should use smart pointers.
When using Qt, especially when using widgets and other QObject
subtypes, some of the memory management is done for
you: https://doc.qt.io/qt-6/objecttrees.html

And generally the whole point of destructors is that you don't
explicitly call them.  They get called automatically at the end of the
lifespan of the value, whether that is the end of the scope for a
stack-allocated object, or the "delete" of a "new"-created object, or
dropping the last reference to a smart pointer (which I expect is really
a new/delete under the covers), or overwriting the variable with a new
value.

On 9/24/2025 8:15 AM, Marius Kintel via Discuss wrote: > On Sep 24, 2025, at 11:02, vulcan_--- via Discuss > <discuss@lists.openscad.org> wrote: >> >> in working with classes like QtFileInfo, QtSaveFile and the like i >> see that they all have destructors like >> >> ~QtFileInfo and ~QtSaveFile >> >> Back when i was writing C++ code for a living we tried to be really >> careful to always invoke the destructors before returning from a >> function. >> >> Do i get to ignore this in modern day C++ coding practice? >> > > Can you give a more concrete example? > In general: If you call “new”, you should call “delete” as well, but > most of the time you should use smart pointers. > When using Qt, especially when using widgets and other QObject > subtypes, some of the memory management is done for > you: https://doc.qt.io/qt-6/objecttrees.html And generally the whole point of destructors is that you *don't* explicitly call them.  They get called automatically at the end of the lifespan of the value, whether that is the end of the scope for a stack-allocated object, or the "delete" of a "new"-created object, or dropping the last reference to a smart pointer (which I expect is really a new/delete under the covers), or overwriting the variable with a new value.
GB
Glenn Butcher
Wed, Sep 24, 2025 7:52 PM

Don't they teach about the heap and stack anymore?

On September 24, 2025 7:58:19 AM HST, Jordan Brown via Discuss discuss@lists.openscad.org wrote:

On 9/24/2025 8:15 AM, Marius Kintel via Discuss wrote:

On Sep 24, 2025, at 11:02, vulcan_--- via Discuss
discuss@lists.openscad.org wrote:

in working with classes like QtFileInfo, QtSaveFile and the like i
see that they all have destructors like

~QtFileInfo and ~QtSaveFile

Back when i was writing C++ code for a living we tried to be really
careful to always invoke the destructors before returning from a
function.

Do i get to ignore this in modern day C++ coding practice?

Can you give a more concrete example?
In general: If you call “new”, you should call “delete” as well, but
most of the time you should use smart pointers.
When using Qt, especially when using widgets and other QObject
subtypes, some of the memory management is done for
you: https://doc.qt.io/qt-6/objecttrees.html

And generally the whole point of destructors is that you don't
explicitly call them.  They get called automatically at the end of the
lifespan of the value, whether that is the end of the scope for a
stack-allocated object, or the "delete" of a "new"-created object, or
dropping the last reference to a smart pointer (which I expect is really
a new/delete under the covers), or overwriting the variable with a new
value.

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Don't they teach about the heap and stack anymore? On September 24, 2025 7:58:19 AM HST, Jordan Brown via Discuss <discuss@lists.openscad.org> wrote: >On 9/24/2025 8:15 AM, Marius Kintel via Discuss wrote: >> On Sep 24, 2025, at 11:02, vulcan_--- via Discuss >> <discuss@lists.openscad.org> wrote: >>> >>> in working with classes like QtFileInfo, QtSaveFile and the like i >>> see that they all have destructors like >>> >>> ~QtFileInfo and ~QtSaveFile >>> >>> Back when i was writing C++ code for a living we tried to be really >>> careful to always invoke the destructors before returning from a >>> function. >>> >>> Do i get to ignore this in modern day C++ coding practice? >>> >> >> Can you give a more concrete example? >> In general: If you call “new”, you should call “delete” as well, but >> most of the time you should use smart pointers. >> When using Qt, especially when using widgets and other QObject >> subtypes, some of the memory management is done for >> you: https://doc.qt.io/qt-6/objecttrees.html > >And generally the whole point of destructors is that you *don't* >explicitly call them.  They get called automatically at the end of the >lifespan of the value, whether that is the end of the scope for a >stack-allocated object, or the "delete" of a "new"-created object, or >dropping the last reference to a smart pointer (which I expect is really >a new/delete under the covers), or overwriting the variable with a new >value. -- Sent from my Android device with K-9 Mail. Please excuse my brevity.
V
vulcan_@mac.com
Wed, Sep 24, 2025 11:31 PM

Marius Kintel wrote:

On Sep 24, 2025, at 11:02, vulcan_--- via Discuss discuss@lists.openscad.org wrote:

in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like

Can you give a more concrete example?
In general: If you call “new”, you should call “delete” as well,

wandering around in the code base i have seen new being used explicitly .. so back in the day that meant that something needed to be done, explicitly, to release the objects created when they stop being needed

so sorry, i don’t recall just now where i saw that .. i have been jumping around so much that i might be conflating scad code with other folks stuff

i was working away on learning how Qt works and remembered having to stress over de-allocating stuff just before every return statement in my code .. but it was a faded memory from long ago

the last time i was coding in C++ was 2002 so i am hoping that things are improved and that instantiated objects get disposed of when they go out of scope .. but that could just be wishful thinking

Marius Kintel wrote: > On Sep 24, 2025, at 11:02, vulcan_--- via Discuss [discuss@lists.openscad.org](mailto:discuss@lists.openscad.org) wrote: > > > in working with classes like QtFileInfo, QtSaveFile and the like i see that they all have destructors like > > Can you give a more concrete example? > In general: If you call “new”, you should call “delete” as well, wandering around in the code base i have seen **new** being used explicitly .. so back in the day that meant that something needed to be done, explicitly, to release the objects created when they stop being needed so sorry, i don’t recall just now where i saw that .. i have been jumping around so much that i might be conflating scad code with other folks stuff i was working away on learning how Qt works and remembered having to stress over de-allocating stuff just before every return statement in my code .. but it was a faded memory from long ago the last time i was coding in C++ was 2002 so i am hoping that things are improved and that instantiated objects get disposed of when they go out of scope .. but that could just be wishful thinking
V
vulcan_@mac.com
Wed, Sep 24, 2025 11:32 PM

Glenn Butcher wrote:

Don't they teach about the heap and stack anymore?

dunno Glenn, i have not been in school since 1980 .. well, not as a student at least.

Glenn Butcher wrote: > Don't they teach about the heap and stack anymore? dunno Glenn, i have not been in school since 1980 .. well, not as a student at least.
JB
Jordan Brown
Thu, Sep 25, 2025 2:52 AM

On 9/24/2025 4:31 PM, vulcan_--- via Discuss wrote:

wandering around in the code base i have seen new being used
explicitly ..

An explicit "new" needs to be paired with an explicit "delete".

But mostly it should be using std::shared_ptr, which is a
reference-counted pointer where the allocated object is automatically
deleted when the last reference is destroyed.

On 9/24/2025 4:31 PM, vulcan_--- via Discuss wrote: > > wandering around in the code base i have seen *new* being used > explicitly .. > An explicit "new" needs to be paired with an explicit "delete". But mostly it should be using std::shared_ptr, which is a reference-counted pointer where the allocated object is automatically deleted when the last reference is destroyed.
CC
Cory Cross
Thu, Sep 25, 2025 11:30 PM

On 9/24/25 4:31 PM, vulcan_--- via Discuss wrote:

wandering around in the code base i have seen new being used explicitly

The codebase is old, so not everything is what it "should" be, hence PRs
with changes which switch from raw pointers to std::shared_ptr
https://github.com/openscad/openscad/pull/6144/commits/829bcbbba14953c269ac788e6c47f593dcfea724.

  • Cory
On 9/24/25 4:31 PM, vulcan_--- via Discuss wrote: > > wandering around in the code base i have seen *new* being used explicitly > The codebase is old, so not everything is what it "should" be, hence PRs with changes which switch from raw pointers to std::shared_ptr <https://github.com/openscad/openscad/pull/6144/commits/829bcbbba14953c269ac788e6c47f593dcfea724>. - Cory