From 0f74c88af6d56bd90a90bf24b749962683223ee4 Mon Sep 17 00:00:00 2001 From: Talyx Date: Sun, 20 Feb 2022 14:47:24 +0300 Subject: [PATCH] add: safe memory free --- src/Client/Client.cpp | 8 ++++++-- src/Client/Request.cpp | 6 +++++- src/Client/Response.cpp | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index dfc6599..b4b437d 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -189,8 +189,7 @@ std::string Client::generateRespons(std::vector &configs) std::cout << GREEN << "Response Header\n{" << ENDL; std::cout << BLUE << _response.getHeader() << GREEN << "}" << PINK << "]]\n"<< ENDL; delete _toSend; - if (_request.getBody() != NULL) - _request.freeData(); + _request.freeData(); _response.freeData(); return (_headerToSend); } @@ -267,7 +266,12 @@ void Client::clear(void) _bodyToSend = ""; _headerToSend = ""; if (_to_send_char) + { delete[] _to_send_char; + _to_send_char = NULL; + } + _request.freeData(); + _response.freeData(); } Client::~Client() diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index cf74ab5..bc5e41c 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -36,7 +36,11 @@ Request::Request(char *str) void Request::freeData(void) { - delete _body; + if (_body != NULL) + { + delete _body; + _body = NULL; + } } std::string &Request::getURI(void) diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index a39a609..6ffb65a 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -16,8 +16,15 @@ Response::Response() void Response::freeData(void) { if (_body != NULL) + { delete _body; - delete _header; + _body = NULL; + } + else if (_header != NULL) + { + delete _header; + _header = NULL; + } } std::string Response::getHeader(void)