Advice wanted on garbage collection of sockets for c++ programmer using D

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 29 13:33:39 PDT 2017


On Wednesday, 28 June 2017 at 15:55:41 UTC, John Burton wrote:
> On Tuesday, 27 June 2017 at 09:54:19 UTC, John Burton wrote:
>> I'm coming from a C++ background so I'm not too used to 
>> garbage collection and it's implications. I have a function 
>> that creates a std.socket.Socket using new and connects to a 
>> tcp server, and writes some stuff to it. I then explicitly 
>> close the socket, and the socket object goes out of scope.
>>
>
>> Am I doing this right? Or is there a better way to do this in 
>> D?
>>
>> Thanks.
>
>
> For my use case here, I'm increasingly thinking that just 
> calling the underlying 'C' socket and send calls is better. No 
> need for anything complicated at all for my actual program :)

One final piece of advice as this thread seemed to have gone off 
the rails a bit. You can always put a `scope(exit) 
socket.close();` after you create the socket. This will ensure 
that the socket will be closed once the scope is exited no matter 
what... almost, anyway. If an Error is thrown no stack unwinding 
is done but at this point your program is in an unrecoverable 
state anyway and you have a lot more to worry about than a socket 
that hasn't been closed.


More information about the Digitalmars-d-learn mailing list