-
Notifications
You must be signed in to change notification settings - Fork 107
Closed
Description
Subject of the issue
Hi,
in the code, there are mismatched deletes.
In C++ new
must match a delete
and new[]
must match delete[]
.
In the code, there are only delete[]
, even for new
.
For example, in file SparkFun_u-blox_GNSS_Arduino_Library.cpp
:
- moduleSWVersion
- currentGeofenceParams
- packetUBXNAVTIMELS
and basically, all struct-based data pointers have this issue.
This mismatch could lead the code to crash, since it goes into undefined behavior.
Regards.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
PaulZC commentedon Jul 19, 2021
Hi Francesco (@FStefanni ),
Thanks for this. I suspect we haven't seen this issue before because on Arduino platforms the implementations of
new
andnew[]
are usually exactly the same. Likewise fordelete
anddelete[]
:Best wishes,
Paul
FStefanni commentedon Jul 20, 2021
Hi,
yes, very often also
malloc
andfree
can be mixed, since the actual implementation is the same (as in the snippet you posted).But to be 100% compliant and portable, one must adhere to the standard.
It is quite annoying to have incompatible memory management methods, and I always hope C++ will fix this in the future... but this is another topic.
Regards.
PaulZC commentedon Aug 7, 2021
Hi Francesco (@FStefanni ),
I've looked into this and I believe I am using the new operator correctly, certainly as far as the compilers used by Arduino are concerned. Please see this link https://www.cplusplus.com/reference/new/operator%20new[]/ , specifically lines 13 and 18 of the example.
In many places, I do create a new array directly:
SparkFun_u-blox_GNSS_Arduino_Library/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp
Line 377 in 46c1855
Both of these alternatives generate compilation errors:
payloadCfg = new[] uint8_t[payloadSize];
payloadCfg = new[payloadSize] uint8_t;
Likewise, where I use typedef struct to define how much memory should be allocated:
SparkFun_u-blox_GNSS_Arduino_Library/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp
Line 4511 in 46c1855
Replacing this with:
moduleSWVersion = new[] moduleSWVersion_t;
also generates a compilation error.
I'm going to close this as I don't believe there is any further action I can take. Please reopen if you can provide code which compiles successfully on Arduino platforms.
Best wishes,
Paul
FStefanni commentedon Aug 9, 2021
Hi,
sorry, maybe I was not clear.
The C++ standards mandates to match the various memory operators. So just to clarify with exampes...
Example 1:
malloc/free
Example 2:
new/delete
Example 3:
new[]/delete[]
Other combinations are not allowed. For example it is not possible to do something as:
If the memory function are mixed, the code will compile, without any warning, but it will be non standard.
More precisely, it will run into the so called "undefined behavior".
So the fix is just to match something as:
with
Regards.
PS: it seems I cannot re-open the issue...
PaulZC commentedon Aug 9, 2021
Hi Francesco (@FStefanni ),
Thank you for the clarification!
As you suggest, I will try replacing
delete[]
withdelete
for those cases where I am creating storage for a struct.Best wishes,
Paul
Replace delete[] with delete - see #53
FStefanni commentedon Aug 9, 2021
Hi,
now it seems fine to me.
Thank you for the fix.
Regards.