Skip to content

Commit dd73a93

Browse files
author
Ari Parkkila
committed
Cellular: Fix CellularContext destructor memory leak
1 parent 998d06a commit dd73a93

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,20 @@ class my_stack : public AT_CellularStack {
109109
class my_AT_CTX : public AT_CellularContext {
110110
public:
111111
my_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) :
112-
AT_CellularContext(at, device, apn), _st(at, *get_device())
112+
AT_CellularContext(at, device, apn)
113113
{
114114
AT_CellularDevice_stub::supported_bool = false;
115115
}
116116
virtual ~my_AT_CTX() {}
117117

118118
virtual NetworkStack *get_stack()
119119
{
120-
return &_st;
120+
if (!_stack) {
121+
_stack = new my_stack(_at, *get_device());
122+
}
123+
return _stack;
121124
}
125+
122126
virtual uint32_t get_timeout_for_operation(ContextOperation op) const
123127
{
124128
return 10;
@@ -137,24 +141,23 @@ class my_AT_CTX : public AT_CellularContext {
137141
{
138142
deactivate_non_ip_context();
139143
}
140-
141-
my_stack _st;
142144
};
143145

144146
class my_AT_CTXIPV6 : public AT_CellularContext {
145147
public:
146148
my_AT_CTXIPV6(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) :
147-
AT_CellularContext(at, device, apn), _st(at, *get_device()) {}
149+
AT_CellularContext(at, device, apn) {}
148150
virtual ~my_AT_CTXIPV6() {}
149151
virtual NetworkStack *get_stack()
150152
{
151-
return &_st;
153+
if (!_stack) {
154+
_stack = new my_stack(_at, *get_device());
155+
}
152156
}
153157
virtual uint32_t get_timeout_for_operation(ContextOperation op) const
154158
{
155159
return 10;
156160
}
157-
my_stack _st;
158161
};
159162

160163
class def_AT_CTX : public AT_CellularContext {

features/cellular/framework/API/CellularContext.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef _CELLULARCONTEXT_H_
1818
#define _CELLULARCONTEXT_H_
1919

20+
#include "NetworkStack.h"
2021
#include "CellularInterface.h"
2122
#include "CellularDevice.h"
2223
#include "CellularUtil.h"
@@ -117,7 +118,15 @@ class CellularContext : public CellularInterface {
117118
// friend of CellularDevice, so it's the only way to close or delete this class.
118119
friend class CellularDevice;
119120
CellularContext();
120-
virtual ~CellularContext() {}
121+
virtual ~CellularContext()
122+
{
123+
#if !NSAPI_PPP_AVAILABLE
124+
if (_stack) {
125+
delete _stack;
126+
}
127+
#endif
128+
}
129+
121130
public: // from NetworkInterface
122131
virtual nsapi_error_t set_blocking(bool blocking) = 0;
123132
virtual NetworkStack *get_stack() = 0;

0 commit comments

Comments
 (0)