Allocate stateful persistance on heap - not stack

This commit is contained in:
Niklas Jensen
2026-01-30 19:04:35 +01:00
committed by nikguin04
parent 2ce29ae0cc
commit bce9041f1f
@@ -46,15 +46,19 @@ class FSPersistencePB {
file.close(); file.close();
if (bytesRead == fileSize) { if (bytesRead == fileSize) {
T protoMsg = {}; // Allocate on heap to avoid stack overflow with large proto messages
T *protoMsg = new T();
*protoMsg = {};
pb_istream_t stream = pb_istream_from_buffer(buffer, bytesRead); pb_istream_t stream = pb_istream_from_buffer(buffer, bytesRead);
if (pb_decode(&stream, _msgDescriptor, &protoMsg)) { if (pb_decode(&stream, _msgDescriptor, protoMsg)) {
_statefulService->updateWithoutPropagation( _statefulService->updateWithoutPropagation(
[this, &protoMsg](T &state) { return _stateUpdater(protoMsg, state); }); [this, protoMsg](T &state) { return _stateUpdater(*protoMsg, state); });
delete protoMsg;
delete[] buffer; delete[] buffer;
return; return;
} }
delete protoMsg;
} }
delete[] buffer; delete[] buffer;
} else { } else {
@@ -70,10 +74,13 @@ class FSPersistencePB {
uint8_t *buffer = new uint8_t[_maxSize]; uint8_t *buffer = new uint8_t[_maxSize];
pb_ostream_t stream = pb_ostream_from_buffer(buffer, _maxSize); pb_ostream_t stream = pb_ostream_from_buffer(buffer, _maxSize);
T protoMsg = {}; // Allocate on heap to avoid stack overflow with large proto messages
_statefulService->read([this, &protoMsg](const T &state) { _stateReader(state, protoMsg); }); T *protoMsg = new T();
*protoMsg = {};
_statefulService->read([this, protoMsg](const T &state) { _stateReader(state, *protoMsg); });
bool encodeSuccess = pb_encode(&stream, _msgDescriptor, &protoMsg); bool encodeSuccess = pb_encode(&stream, _msgDescriptor, protoMsg);
delete protoMsg;
if (!encodeSuccess) { if (!encodeSuccess) {
delete[] buffer; delete[] buffer;