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();
uWS::Loop::defaultLoop()->free();
// return 0;
// 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 */
HttpContext<SSL> *httpContext;
std::vector<WebSocketContext<SSL, true> *> webSocketContexts;
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
using StaticDispatch<SSL>::static_dispatch;
public:
@@ -57,6 +59,10 @@ public:
~TemplatedApp() {
/* Let's just put everything here */
httpContext->free();
for (auto *webSocketContext : webSocketContexts) {
webSocketContext->free();
}
}
TemplatedApp(const TemplatedApp &other) {
@@ -83,6 +89,9 @@ public:
/* 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);
/* We need to clear this later on */
webSocketContexts.push_back(webSocketContext);
/* Quick fix to disable any compression if set */
#ifdef UWS_NO_ZLIB
behavior.compression = uWS::DISABLED;
@@ -184,6 +193,7 @@ public:
}
});
// never called
return *this;
}
+8
View File
@@ -62,6 +62,10 @@ private:
Loop() = delete;
~Loop() {
std::cout << "Loop destructor called" << std::endl;
}
Loop *init() {
new (us_loop_ext((us_loop *) this)) LoopData;
return this;
@@ -96,7 +100,11 @@ public:
/* Freeing the default loop should be done once */
void free() {
LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this);
loopData->~LoopData();
us_loop_free((us_loop *) this);
std::cout << "Loop::free" << std::endl;
}
/* Set postCb callback */
+10
View File
@@ -38,6 +38,16 @@ private:
std::function<void(Loop *)> postHandler;
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. */
static const int CORK_BUFFER_SIZE = 16 * 1024;
+1
View File
@@ -124,6 +124,7 @@ struct InflationStream {
~InflationStream() {
std::cout << "Destructing inflationstream" << std::endl;
inflateEnd(&inflationStream);
}
std::string_view inflate(ZlibContext *zlibContext, std::string_view compressed, size_t maxPayloadLength) {
+9
View File
@@ -335,6 +335,15 @@ private:
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:
/* 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) {