BusFault Crash When Calling handleParseFailure()
I’m having an issue where simply calling a function causes a crash.
I have reset reason turned on and it prints this upon checking on reboot:
Reset Reason: 130, Data: 4
Which means:
RESET_REASON_PANIC, BusFault
Full Code Block for Reference
void ScaleDevice::handleReadResponse(CommandHandler &handler)
{
Log.info("B1");
if (handler.hasTimedOut())
{
Log.info("B2 timed out");
FaultLight::setFault(faultLightCodes::connectionError);
std::string timeoutMsg = handler.handleTimeout();
deviceLogger(LOG_LEVEL_WARN, "%s", timeoutMsg);
return;
}
if (!indicatorComm->available())
{
Log.info("B3 no data");
return;
}
std::string response = indicatorComm->read();
Log.info("B4 response='%s'", response.c_str());
if (response.empty())
{
Log.info("B5 empty");
return;
}
Log.info("B6 pre-parsing");
Result preParsedStr = indicator->preParsing(response);
Log.info("B7 preParse success=%d data='%s' err='%s'",
preParsedStr.success,
preParsedStr.data.c_str(),
preParsedStr.errorMessage.c_str());
if (!preParsedStr.success)
{
Log.info("B8 parse failed path");
std::string parseFailMsg = handler.handleParseFailure();
Log.info("B9");
deviceLogger(LOG_LEVEL_WARN, "%s", parseFailMsg);
FaultLight::setFault(faultLightCodes::badParseReponse);
return;
}
if (preParsedStr.data.empty())
{
Log.info("B10 empty after parse");
std::string parseFailMsg = handler.handleParseFailure();
deviceLogger(LOG_LEVEL_WARN, "%s", parseFailMsg);
return;
}
Log.info("B11 processing valid response");
processValidResponse(handler, preParsedStr.data.c_str());
Log.info("B12 done");
}
Where the Crash Happens
The issue arises when pre-parsing fails and execution enters this block:
if (!preParsedStr.success)
{
Log.info("B8 parse failed path");
std::string parseFailMsg = handler.handleParseFailure();
Log.info("B9");
deviceLogger(LOG_LEVEL_WARN, "%s", parseFailMsg);
FaultLight::setFault(faultLightCodes::badParseReponse);
return;
}
It always logs everything up to:
B8 parse failed path
However, when it enters handleParseFailure(), it never seems to even run the first line of code in that function. Even if I place a log statement as the first line, it never prints.
handleParseFailure() Implementation
std::string handleParseFailure()
{
Log.info("HPF enter");
if (!currentCommand)
{
Log.info("HPF no cmd");
return "No Current Command";
}
Log.info("HPF1");
std::string name = currentCommand->getCommandName();
Log.info("HPF2 name=%s", name.c_str());
std::string failResult = currentCommand->handleFailure();
Log.info("HPF3 fail=%s", failResult.c_str());
if (currentCommand->getState() == CommandState::FAILED)
{
Log.info("HPF4 failing");
failCommand();
Log.info("HPF5 failed");
}
else
{
Log.info("HPF4 retry");
currentCommand->setState(CommandState::READY_TO_SEND);
Log.info("HPF5 reset");
}
return "handled: " + name;
}
Observations
handleris valid at this point.- I can successfully call other functions on it right before
handleParseFailure()(e.g., getting command queue size). freeMemory()shows there is plenty of memory available.- If I move all the code from
handleParseFailure()directly intohandleReadResponse()and replace the function call with inline logic, everything works. - The crash only occurs when calling the function.
Current Theory
My only remaining lead is that this might be a heap memory issue — but I’ve tried everything I can think of.