Fix more memory leaks

This commit is contained in:
Alex Hultman
2018-12-30 17:43:11 +01:00
parent 6618f7194b
commit 339e2d72dc
6 changed files with 40 additions and 0 deletions
+2
View File
@@ -67,6 +67,8 @@ int main(int argc, char **argv) {
} }
}).run(); }).run();
uWS::Loop::defaultLoop()->free();
// return 0; // return 0;
// AsyncFileStreamer *asyncFileStreamer = new AsyncFileStreamer("/home/alexhultman/v0.15/public"); // AsyncFileStreamer *asyncFileStreamer = new AsyncFileStreamer("/home/alexhultman/v0.15/public");
+10
View File
@@ -46,6 +46,8 @@ private:
/* The app always owns at least one http context, but creates websocket contexts on demand */ /* The app always owns at least one http context, but creates websocket contexts on demand */
HttpContext<SSL> *httpContext; HttpContext<SSL> *httpContext;
std::vector<WebSocketContext<SSL, true> *> webSocketContexts;
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE; using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
using StaticDispatch<SSL>::static_dispatch; using StaticDispatch<SSL>::static_dispatch;
public: public:
@@ -57,6 +59,10 @@ public:
~TemplatedApp() { ~TemplatedApp() {
/* Let's just put everything here */ /* Let's just put everything here */
httpContext->free(); httpContext->free();
for (auto *webSocketContext : webSocketContexts) {
webSocketContext->free();
}
} }
TemplatedApp(const TemplatedApp &other) { TemplatedApp(const TemplatedApp &other) {
@@ -83,6 +89,9 @@ public:
/* Every route has its own websocket context with its own behavior and user data type */ /* Every route has its own websocket context with its own behavior and user data type */
auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext); auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
/* We need to clear this later on */
webSocketContexts.push_back(webSocketContext);
/* Quick fix to disable any compression if set */ /* Quick fix to disable any compression if set */
#ifdef UWS_NO_ZLIB #ifdef UWS_NO_ZLIB
behavior.compression = uWS::DISABLED; behavior.compression = uWS::DISABLED;
@@ -184,6 +193,7 @@ public:
} }
}); });
// never called
return *this; return *this;
} }
+8
View File
@@ -62,6 +62,10 @@ private:
Loop() = delete; Loop() = delete;
~Loop() {
std::cout << "Loop destructor called" << std::endl;
}
Loop *init() { Loop *init() {
new (us_loop_ext((us_loop *) this)) LoopData; new (us_loop_ext((us_loop *) this)) LoopData;
return this; return this;
@@ -96,7 +100,11 @@ public:
/* Freeing the default loop should be done once */ /* Freeing the default loop should be done once */
void free() { void free() {
LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this);
loopData->~LoopData();
us_loop_free((us_loop *) this); us_loop_free((us_loop *) this);
std::cout << "Loop::free" << std::endl;
} }
/* Set postCb callback */ /* Set postCb callback */
+10
View File
@@ -38,6 +38,16 @@ private:
std::function<void(Loop *)> postHandler; std::function<void(Loop *)> postHandler;
public: public:
~LoopData() {
/* If we have had App.ws called with compression we need to clear this */
if (zlibContext) {
delete zlibContext;
delete inflationStream;
delete deflationStream;
}
delete [] corkBuffer;
}
/* Good 16k for SSL perf. */ /* Good 16k for SSL perf. */
static const int CORK_BUFFER_SIZE = 16 * 1024; static const int CORK_BUFFER_SIZE = 16 * 1024;
+1
View File
@@ -124,6 +124,7 @@ struct InflationStream {
~InflationStream() { ~InflationStream() {
std::cout << "Destructing inflationstream" << std::endl; std::cout << "Destructing inflationstream" << std::endl;
inflateEnd(&inflationStream);
} }
std::string_view inflate(ZlibContext *zlibContext, std::string_view compressed, size_t maxPayloadLength) { std::string_view inflate(ZlibContext *zlibContext, std::string_view compressed, size_t maxPayloadLength) {
+9
View File
@@ -335,6 +335,15 @@ private:
return this; return this;
} }
void free() {
std::cout << "websocket context free" << std::endl;
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_socket_context_ext((SOCKET_CONTEXT_TYPE *) this);
webSocketContextData->~WebSocketContextData();
us_socket_context_free((SOCKET_CONTEXT_TYPE *) this);
}
public: public:
/* WebSocket contexts are always child contexts to a HTTP context so no SSL options are needed as they are inherited */ /* WebSocket contexts are always child contexts to a HTTP context so no SSL options are needed as they are inherited */
static WebSocketContext *create(Loop *loop, SOCKET_CONTEXT_TYPE *parentSocketContext) { static WebSocketContext *create(Loop *loop, SOCKET_CONTEXT_TYPE *parentSocketContext) {