Skip to content

Commit 095f7aa

Browse files
authored
Merge pull request #75 from LeeLeahy2/payload-auto
GNSS: Keep payloadAuto allocated until destructor, reallocate as needed
2 parents dce0d86 + 58dcf7d commit 095f7aa

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/u-blox_GNSS.cpp

+21-17
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
15921592
// by other threads without overwriting the requested / expected Class and ID.
15931593
volatile static uint8_t storedClass = 0;
15941594
volatile static uint8_t storedID = 0;
1595+
static size_t payloadAutoBytes;
15951596
if (requestedClass || requestedID) // If either is non-zero, store the requested Class and ID
15961597
{
15971598
storedClass = requestedClass;
@@ -1695,21 +1696,28 @@ void DevUBLOXGNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t req
16951696
}
16961697
#endif
16971698
}
1698-
if (payloadAuto != nullptr) // Check if memory is already allocated - this should be impossible!
1699+
1700+
// Determine the payload length
1701+
if ((!logBecauseAuto) && (logBecauseEnabled))
1702+
maxPayload = SFE_UBX_MAX_LENGTH;
1703+
1704+
// Increase the payloadAuto buffer size if necessary, by removing
1705+
// the previous buffer
1706+
if (payloadAuto && (payloadAutoBytes < maxPayload))
16991707
{
1700-
#ifndef SFE_UBLOX_REDUCED_PROG_MEM
1701-
if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
1702-
{
1703-
_debugSerial.println(F("process: memory is already allocated for payloadAuto! Deleting..."));
1704-
}
1705-
#endif
1706-
delete[] payloadAuto; // Created with new[]
1708+
delete[] payloadAuto; // Created with new[] below
17071709
payloadAuto = nullptr;
1708-
packetAuto.payload = payloadAuto;
1710+
payloadAutoBytes = 0;
17091711
}
1710-
if ((!logBecauseAuto) && (logBecauseEnabled))
1711-
maxPayload = SFE_UBX_MAX_LENGTH;
1712-
payloadAuto = new uint8_t[maxPayload]; // Allocate RAM for payloadAuto
1712+
1713+
// Allocate the payloadAuto buffer if necessary
1714+
if (payloadAuto == nullptr)
1715+
{
1716+
payloadAuto = new uint8_t[maxPayload];
1717+
if (payloadAuto)
1718+
payloadAutoBytes = maxPayload;
1719+
}
1720+
17131721
packetAuto.payload = payloadAuto;
17141722
if (payloadAuto == nullptr) // Check if the alloc failed
17151723
{
@@ -3271,11 +3279,7 @@ void DevUBLOXGNSS::processUBX(uint8_t incoming, ubxPacket *incomingUBX, uint8_t
32713279
// Now that the packet is complete and has been processed, we need to delete the memory
32723280
// allocated for packetAuto
32733281
if (activePacketBuffer == SFE_UBLOX_PACKET_PACKETAUTO)
3274-
{
3275-
delete[] payloadAuto; // Created with new[]
3276-
payloadAuto = nullptr;
3277-
packetAuto.payload = payloadAuto;
3278-
}
3282+
packetAuto.payload = nullptr;
32793283
}
32803284
else // Load this byte into the payload array
32813285
{

0 commit comments

Comments
 (0)