@@ -26,6 +26,22 @@ inline size_t saturatingSubtract(size_t value, size_t delta) {
2626 return value > delta ? value - delta : 0 ;
2727}
2828
29+ template <typename Callback, typename ... Args>
30+ void invokeMemoryMonitorCallback (const Callback &callback, Args... args) noexcept {
31+ if (!callback) {
32+ return ;
33+ }
34+
35+ #if defined(__cpp_exceptions)
36+ try {
37+ callback (args...);
38+ } catch (...) {
39+ }
40+ #else
41+ callback (args...);
42+ #endif
43+ }
44+
2945} // namespace
3046
3147ESPMemoryMonitor *gPanicInstance = nullptr ;
@@ -230,18 +246,18 @@ MemorySnapshot ESPMemoryMonitor::sampleNow() {
230246
231247 for (const auto &evt : events) {
232248 if (thresholdCb) {
233- thresholdCb ( evt);
249+ invokeMemoryMonitorCallback (thresholdCb, evt);
234250 }
235251 }
236252
237253 for (const auto &evt : stackEvents) {
238254 if (stackCb) {
239- stackCb ( evt);
255+ invokeMemoryMonitorCallback (stackCb, evt);
240256 }
241257 }
242258
243259 if (sampleCb) {
244- sampleCb ( publicSnapshot);
260+ invokeMemoryMonitorCallback (sampleCb, publicSnapshot);
245261 }
246262
247263 return publicSnapshot;
@@ -369,7 +385,7 @@ LeakCheckResult ESPMemoryMonitor::markLeakCheckPoint(const std::string &label) {
369385 LeakCheckResult result = toPublicLeakCheckResult (internal);
370386
371387 if (cb && !result.deltas .empty ()) {
372- cb ( result);
388+ invokeMemoryMonitorCallback (cb, result);
373389 }
374390
375391 return result;
@@ -547,12 +563,12 @@ ScopeStats ESPMemoryMonitor::finalizeScope(const MemoryScope &scope) {
547563 }
548564
549565 if (scopeCb) {
550- scopeCb ( toPublicScopeStats (stats));
566+ invokeMemoryMonitorCallback (scopeCb, toPublicScopeStats (stats));
551567 }
552568
553569 if (tagCb) {
554570 for (const auto &evt : tagEvents) {
555- tagCb ( evt);
571+ invokeMemoryMonitorCallback (tagCb, evt);
556572 }
557573 }
558574
@@ -716,7 +732,7 @@ void ESPMemoryMonitor::handleAllocEvent(
716732 event.functionName = functionName;
717733 event.timestampUs = esp_timer_get_time ();
718734
719- cb ( event);
735+ invokeMemoryMonitorCallback (cb, event);
720736}
721737
722738void ESPMemoryMonitor::allocFailedHook (
@@ -1189,7 +1205,7 @@ void ESPMemoryMonitor::runPanicHook() {
11891205 }
11901206
11911207 if (cb) {
1192- cb ( snapshot);
1208+ invokeMemoryMonitorCallback (cb, snapshot);
11931209 }
11941210}
11951211
0 commit comments