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:
socket
must be closed.connect
, listen
or accept
.The man pages for socket
& friends and close
are not very clear (or at least to me).
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.
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.
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With