From 150ada2f4ec2dd89900062d30871b0ee2b84a507 Mon Sep 17 00:00:00 2001 From: Samy BEN TAYEB Date: Wed, 18 Jun 2025 00:20:54 +0200 Subject: [PATCH] segfault in function JOIN Fixed --- include/channel.hpp | 4 ++-- sources/channel/channel.cpp | 4 ++-- sources/commands/join.cpp | 5 ++--- sources/core/Server.cpp | 15 +++++++-------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/include/channel.hpp b/include/channel.hpp index cd10c89..16a042f 100644 --- a/include/channel.hpp +++ b/include/channel.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 22:18:17 by rparodi #+# #+# */ -/* Updated: 2025/06/17 23:38:49 by sben-tay ### ########.fr */ +/* Updated: 2025/06/18 00:10:56 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ class Channel { size_t getMaxUsers() const; User *getOwner() const; std::list getOperators() const; - std::list getUsers() const; + std::list& getUsers(); std::list getInvited() const; std::string getPassword() const; bool isOperator(User *user) const; diff --git a/sources/channel/channel.cpp b/sources/channel/channel.cpp index cc0e044..fbbaede 100644 --- a/sources/channel/channel.cpp +++ b/sources/channel/channel.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */ -/* Updated: 2025/06/17 23:51:54 by sben-tay ### ########.fr */ +/* Updated: 2025/06/18 00:10:48 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ User *Channel::getOwner() const { * * @return list of Users in the channel */ -std::list Channel::getUsers() const { +std::list& Channel::getUsers() { return this->_users; } diff --git a/sources/commands/join.cpp b/sources/commands/join.cpp index a5d5650..c81392e 100644 --- a/sources/commands/join.cpp +++ b/sources/commands/join.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/17 23:50:28 by sben-tay ### ########.fr */ +/* Updated: 2025/06/17 23:55:31 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,5 +89,4 @@ void Join::execute() { msg353 += (*it)->getNickname() + " "; } _cTarget->sendAllClientInAChannel(msgJoin + msg332 + msg353 + "\r\n"); - -} \ No newline at end of file +} diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index 47bdb02..4b49c8b 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/06/17 23:16:55 by sben-tay ### ########.fr */ +/* Updated: 2025/06/18 00:10:14 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -185,7 +185,6 @@ void Server::printUsers() const std::string Server::getPassword() const { return this->_password; } std::list Server::getUsersList() const { - // to_delete when done WARNING_MSG("TO DO FILL") std::list userList; for (std::map::const_iterator it = _users.begin(); it != _users.end(); ++it) { @@ -212,10 +211,10 @@ std::vector splitLines(const std::string& input) { } void Server::disconnectClient(int fd) { - _pollManager.removeClient(fd); - close(fd); - if (_users.count(fd)) { - delete _users[fd]; - _users.erase(fd); - } + User *user = _users[fd]; + for (std::list::iterator it = _channels.begin(); it != _channels.end(); ++it) + (*it)->removeUser(user); + + delete user; + _users.erase(fd); } \ No newline at end of file