From dd73a93887d39d849f1823e59bb0950a3bbcc4ba Mon Sep 17 00:00:00 2001 From: Ari Parkkila Date: Wed, 10 Jun 2020 22:21:48 -0700 Subject: [PATCH] Cellular: Fix CellularContext destructor memory leak --- .../at_cellularcontexttest.cpp | 17 ++++++++++------- .../cellular/framework/API/CellularContext.h | 11 ++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp index fbcbadd942..304e342d3f 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp @@ -109,7 +109,7 @@ public: class my_AT_CTX : public AT_CellularContext { public: my_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : - AT_CellularContext(at, device, apn), _st(at, *get_device()) + AT_CellularContext(at, device, apn) { AT_CellularDevice_stub::supported_bool = false; } @@ -117,8 +117,12 @@ public: virtual NetworkStack *get_stack() { - return &_st; + if (!_stack) { + _stack = new my_stack(_at, *get_device()); + } + return _stack; } + virtual uint32_t get_timeout_for_operation(ContextOperation op) const { return 10; @@ -137,24 +141,23 @@ public: { deactivate_non_ip_context(); } - - my_stack _st; }; class my_AT_CTXIPV6 : public AT_CellularContext { public: my_AT_CTXIPV6(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : - AT_CellularContext(at, device, apn), _st(at, *get_device()) {} + AT_CellularContext(at, device, apn) {} virtual ~my_AT_CTXIPV6() {} virtual NetworkStack *get_stack() { - return &_st; + if (!_stack) { + _stack = new my_stack(_at, *get_device()); + } } virtual uint32_t get_timeout_for_operation(ContextOperation op) const { return 10; } - my_stack _st; }; class def_AT_CTX : public AT_CellularContext { diff --git a/features/cellular/framework/API/CellularContext.h b/features/cellular/framework/API/CellularContext.h index 8519abaa93..1b97820869 100644 --- a/features/cellular/framework/API/CellularContext.h +++ b/features/cellular/framework/API/CellularContext.h @@ -17,6 +17,7 @@ #ifndef _CELLULARCONTEXT_H_ #define _CELLULARCONTEXT_H_ +#include "NetworkStack.h" #include "CellularInterface.h" #include "CellularDevice.h" #include "CellularUtil.h" @@ -117,7 +118,15 @@ protected: // friend of CellularDevice, so it's the only way to close or delete this class. friend class CellularDevice; CellularContext(); - virtual ~CellularContext() {} + virtual ~CellularContext() + { +#if !NSAPI_PPP_AVAILABLE + if (_stack) { + delete _stack; + } +#endif + } + public: // from NetworkInterface virtual nsapi_error_t set_blocking(bool blocking) = 0; virtual NetworkStack *get_stack() = 0;