Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RAII sockets: when to release (close)

I want to write a simple, small C++ RAII wrapper over a C socket.

The question is in what state is a socket considered initialized (from the perspective of RAII) and so eligible for release.

For instance, for a TCP client socket: if the socket call succeed, but the connect call failed, should close be called?

This is just an example, I am interested in a general answer, something like:

  • Each socket successfully created by socket must be closed.
    or
  • There must be a close for each connect, listen or accept.

The man pages for socket & friends and close are not very clear (or at least to me).

like image 684
bolov Avatar asked Apr 27 '15 17:04

bolov


People also ask

How does RAII work?

The principle that objects own resources is also known as "resource acquisition is initialization," or RAII. When a resource-owning stack object goes out of scope, its destructor is automatically invoked. In this way, garbage collection in C++ is closely related to object lifetime, and is deterministic.

What is RAII style?

Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically-typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tied to object lifetime.

What is the use of RAII in C++ programing?

Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the ...


1 Answers

The two parts to pair up for sockets are socket() with close() and connect() with shutdown(). As you see, it doesn't get as easy as with malloc() and free(). This is further complicated by the fact that not every socket is used to connect(), some also use bind() and accept() instead. However, if you call close() without shutdown(), it's just a forceful shutdown that is experienced as an error by the remote side, but you correctly release resources that were allocated.

I'd consider wrapping it twice, once to invoke close() and another time to invoke shutdown(). I wouldn't worry too much about the second part though, as failure to shutdown() is still mostly harmless.

like image 147
Ulrich Eckhardt Avatar answered Oct 27 '22 00:10

Ulrich Eckhardt