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

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 27 19:13:10 PDT 2017


On Wednesday, June 28, 2017 01:11:35 Moritz Maxeiner via Digitalmars-d-learn 
wrote:
> On Wednesday, 28 June 2017 at 00:05:20 UTC, Guillaume Piolat
>
> wrote:
> > On Tuesday, 27 June 2017 at 23:54:50 UTC, Moritz Maxeiner wrote:
> >> - Replace calls by the GC to `~this` with calls to `finalize`
> >> (or invent some cool other shortened name for the latter)
> >
> > My point is that in such a "finalize()" function the only sane
> > things to do is to crash if the resource wasn't freed already.
> > Why so?
> >
> > [...]
>
> Not every class can't be finalized, so it might make sense for
> finalization to remain an available option.

There are definitely cases where finalizers make sense. Case in point: if
you have a socket class, it makes perfect sense for it to have a finalizer.
Yes, it's better to close it manually, but it will work just fine for the GC
to close it when finalizing the class object so long as you don't use so
many sockets that you run out before the GC collects them. So, having a
finalizer is a good backup to ensure that the socket resource doesn't leak.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list