Cellular: Fix CellularContext destructor memory leak

pull/13098/head
Ari Parkkila 2020-06-10 22:21:48 -07:00
parent 998d06a80b
commit dd73a93887
2 changed files with 20 additions and 8 deletions

View File

@ -109,7 +109,7 @@ public:
class my_AT_CTX : public AT_CellularContext { class my_AT_CTX : public AT_CellularContext {
public: public:
my_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : 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; AT_CellularDevice_stub::supported_bool = false;
} }
@ -117,8 +117,12 @@ public:
virtual NetworkStack *get_stack() 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 virtual uint32_t get_timeout_for_operation(ContextOperation op) const
{ {
return 10; return 10;
@ -137,24 +141,23 @@ public:
{ {
deactivate_non_ip_context(); deactivate_non_ip_context();
} }
my_stack _st;
}; };
class my_AT_CTXIPV6 : public AT_CellularContext { class my_AT_CTXIPV6 : public AT_CellularContext {
public: public:
my_AT_CTXIPV6(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : 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 ~my_AT_CTXIPV6() {}
virtual NetworkStack *get_stack() 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 virtual uint32_t get_timeout_for_operation(ContextOperation op) const
{ {
return 10; return 10;
} }
my_stack _st;
}; };
class def_AT_CTX : public AT_CellularContext { class def_AT_CTX : public AT_CellularContext {

View File

@ -17,6 +17,7 @@
#ifndef _CELLULARCONTEXT_H_ #ifndef _CELLULARCONTEXT_H_
#define _CELLULARCONTEXT_H_ #define _CELLULARCONTEXT_H_
#include "NetworkStack.h"
#include "CellularInterface.h" #include "CellularInterface.h"
#include "CellularDevice.h" #include "CellularDevice.h"
#include "CellularUtil.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 of CellularDevice, so it's the only way to close or delete this class.
friend class CellularDevice; friend class CellularDevice;
CellularContext(); CellularContext();
virtual ~CellularContext() {} virtual ~CellularContext()
{
#if !NSAPI_PPP_AVAILABLE
if (_stack) {
delete _stack;
}
#endif
}
public: // from NetworkInterface public: // from NetworkInterface
virtual nsapi_error_t set_blocking(bool blocking) = 0; virtual nsapi_error_t set_blocking(bool blocking) = 0;
virtual NetworkStack *get_stack() = 0; virtual NetworkStack *get_stack() = 0;