Skip to content

fix: fix valgrind case of memory leak when last channel client uses QUIT cmd #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/commands/Quit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 08:29:45 by gilmar #+# #+# */
/* Updated: 2024/05/30 13:06:28 by yde-goes ### ########.fr */
/* Updated: 2024/05/30 14:17:20 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,6 +25,9 @@
* This method processes the QUIT command received from the client and sends a response
* to the client indicating that the client has quit the server.
*
* To avoid memory leak, if the client being disconnected is the last one in a channel,
* the channel is deleted.
*
* @param buffer The buffer containing the QUIT command parameters.
* @param fd The file descriptor associated with the client that sent the command.
*/
Expand All @@ -35,12 +38,18 @@ void Server::_handler_client_quit(const std::string &/* buffer */, const int fd)
for (std::vector<Channel *>::iterator it = _channels.begin(); it != _channels.end(); ++it)
{
Channel *channel = *it;
if (channel->has_client(client))
{
if (channel->has_client(client))
{
channel->part(client);
}
if (channel->get_channel_clients().size() == 0)
delete channel;
}
}

_send_response(fd, RPL_QUITMESSAGE(client->get_nickname()));
_reply_code = 301;

std::cout << RED << "Client <" << fd << "> Disconnected" << WHI << std::endl;
_clear_client(fd);
close(fd);
}