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?
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 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.
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.
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
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.
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/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.