add: safe memory free

This commit is contained in:
Talyx
2022-02-20 14:47:24 +03:00
parent 5c2888190c
commit 0f74c88af6
3 changed files with 19 additions and 4 deletions

View File

@@ -189,8 +189,7 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
std::cout << GREEN << "Response Header\n{" << ENDL; std::cout << GREEN << "Response Header\n{" << ENDL;
std::cout << BLUE << _response.getHeader() << GREEN << "}" << PINK << "]]\n"<< ENDL; std::cout << BLUE << _response.getHeader() << GREEN << "}" << PINK << "]]\n"<< ENDL;
delete _toSend; delete _toSend;
if (_request.getBody() != NULL) _request.freeData();
_request.freeData();
_response.freeData(); _response.freeData();
return (_headerToSend); return (_headerToSend);
} }
@@ -267,7 +266,12 @@ void Client::clear(void)
_bodyToSend = ""; _bodyToSend = "";
_headerToSend = ""; _headerToSend = "";
if (_to_send_char) if (_to_send_char)
{
delete[] _to_send_char; delete[] _to_send_char;
_to_send_char = NULL;
}
_request.freeData();
_response.freeData();
} }
Client::~Client() Client::~Client()

View File

@@ -36,7 +36,11 @@ Request::Request(char *str)
void Request::freeData(void) void Request::freeData(void)
{ {
delete _body; if (_body != NULL)
{
delete _body;
_body = NULL;
}
} }
std::string &Request::getURI(void) std::string &Request::getURI(void)

View File

@@ -16,8 +16,15 @@ Response::Response()
void Response::freeData(void) void Response::freeData(void)
{ {
if (_body != NULL) if (_body != NULL)
{
delete _body; delete _body;
delete _header; _body = NULL;
}
else if (_header != NULL)
{
delete _header;
_header = NULL;
}
} }
std::string Response::getHeader(void) std::string Response::getHeader(void)