I am using Hideataki's MsgPack library to do COBS decoding on some sensor data that I would like to send reliably and efficiently over BLE. The malloc error typically occurs after some 10 minutes of program execution. I'm fairly certain there are no buffer overflows or any thing of that sort, as I am tracking the size of my serial buffer.
Wondering if maybe FreeRTOS is doing a malloc in one thread while MsgPack is also doing a malloc in another or something along these lines. Just a blind stab in the dark. Really not sure.
Maybe someone can help me zero in on some possibilities.
Just to be crystal clear. I have one BLE feather device sending sensor data, and this BLE feather device receiving it and then (when I'm done with the code) sending it over a wired USB serial connection to my laptop.
Here's the most relevant bit of code.
Code: Select all
void bleuart_rx_callback(BLEClientUart& uart_svc)
{
Serial.print("[RX]: ");
while ( uart_svc.available() )
{
//char c = uart_svc.read();
String str = uart_svc.readString();
for (int i = 0; i < str.length(); i++)
{
raw_buffer.data.push_back((uint8_t)str.charAt(i));
}
Serial.print("Raw buffer size: "); Serial.println(raw_buffer.data.size());
//If last char is '0', zero char, then decode buffer.
if (raw_buffer.data.at(raw_buffer.data.size()-1) == 0)
{
const auto& decoded = Packetizer::decode(raw_buffer.data.data(), raw_buffer.data.size(), true, true);
int CRC_received = 0;
if (raw_buffer.data.size() > 2) { CRC_received = raw_buffer.data.at(static_cast<int>(raw_buffer.data.size()) - 2); }
int CRC_expected = (int)crcx::crc8( decoded.data.data(), decoded.data.size() );
if (CRC_expected == 0) {CRC_expected = 1;} //<-- COBS Quirk, can't do zero.
if (CRC_received != CRC_expected) {total_crc_errors++; Serial.println("CRC Error!");}
else
{
//If we got a good CRC, then process.
MsgPack::Unpacker unpacker;
unpacker.feed(decoded.data.data(), decoded.data.size());
unpacker.deserialize( fiber_floats );
Serial.print("CRC Errors = "); Serial.println(total_crc_errors);
Serial.print("Size of data = "); Serial.println(decoded.data.size());
Serial.print("Index = "); Serial.println(decoded.index);
for (int i = 0; i < fiber_floats.size(); i++)
{
Serial.print( "float"); Serial.print(i); Serial.print("[" ); Serial.print(fiber_floats[i]); Serial.println("]");
send_floats.push_back(fiber_floats[i]);
}
//NOW SEND, FOR REAL:
//MsgPack::Packer send_pack;
//MsgPack::arr_t<float> send_floats;
//packer.serialize(send_floats);
//send_floats.erase();
//Serial.print("ENCODED ["); Serial.print(send_floats); Serial.println("]");
}
//Either way, we should clear the buffer after; whether it's bad or good.
raw_buffer.data.clear();
}
}
Serial.println();
}