Ist das Löschen des Zeigers eines Epoll_Event sicher oder könnte dies zu einer Verwendung nach freier Verwendung führen?C++

Programme in C++. Entwicklerforum
Anonymous
 Ist das Löschen des Zeigers eines Epoll_Event sicher oder könnte dies zu einer Verwendung nach freier Verwendung führen?

Post by Anonymous »

Ich schreibe einen epollbasierten TCP -Server in C ++ und repräsentiere derzeit neue Verbindungen als Objekt: < /p>

Code: Select all

Connection::Connection(const int epoll, const int socket): m_socket{socket}, m_epoll{epoll} {
int flags = fcntl(m_socket, F_GETFL, 0);
if (flags == -1) {
throw std::system_error(errno, std::system_category(), "Connection::Connection fcntl");
}

flags |= O_NONBLOCK;
if (fcntl(m_socket, F_SETFL, flags) == -1) {
throw std::system_error(errno, std::system_category(), "Connection::Connection fcntl");
}

epoll_event event{};
event.events = EPOLLIN;
event.data.ptr = this;
if (epoll_ctl(m_epoll, EPOLL_CTL_ADD, m_socket, &event) == -1) {
throw std::system_error(errno, std::system_category(), "Connection::Connection epoll_ctl");
}
}

Connection::~Connection() {
if (epoll_ctl(m_epoll, EPOLL_CTL_DEL, m_socket, nullptr) == -1) {
throw std::system_error(errno, std::system_category(), "Connection::~Connection epoll_ctl");
}

if (close(m_socket) == -1) {
throw std::system_error(errno, std::system_category(), "Connection::~Connection close");
}
}

void Connection::handle_receive() {
const ssize_t bytes_received = recv(m_socket, m_read_buffer.data(), buffer_size, 0);
if (bytes_received == -1) {
// if recv fails for normal reason (read not actually available)
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return;
}
}

// either the connection was closed gracefully by the client, or the connection is broken in some way
// either way we want to delete the connection
if (bytes_received

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post