[Gambas-user] DnsClient example: never ever use _free(), right?

Benoît Minisini gambas at ...1...
Wed Jul 23 23:27:38 CEST 2014


Le 23/07/2014 23:12, Tobias Boege a écrit :
> Hi,
>
> our Gambas book has DnsClient on top of its schedule now as it seems. So I
> looked at the DnsClient example -- for the first time I guess. I found one
> thing in Form_Close() which surprised me (there is actually only one thing
> in there): the use of DnsClient._free().
>
> I always thought you would _never ever_ directly call _free() from an object
> (unless maybe you are the very person who wrote the _free() code and love
> the danger).
>
> In my understanding, _free() is a method only for use by the interpreter
> _after_ an object got a zero reference count and needs to be destroyed.
> Therefore it is not documented and not generally safe to use when the
> reference count is non-zero, i.e. when the object exists for the Gambas
> process, i.e. when you are able to call _free() from Gambas code.
>
> So, when you are able to call _free(), you shouldn't.
>
> The two solutions I see:
>
>   1. Implement a proper "almost-free" method in DnsClient;
>   2. substitute hDnsClient._free() by hDnsClient = Null;
>
> where #2 is a workaround which is IMHO cleaner than calling _free() and #1
> tackles the root cause of why the author resorts to _free(): otherwise the
> program would not terminate after closing the window because there are
> watches as soon as hDnsClient.Async is True and hDnsClient is referenced
> by the FMain object (which is where solution 1 operates).
>
> Since DnsClient is kind of Stream-like, I could imagine a Close() method
> (different from Stop()!) which shuts down those watches -- and the watches
> should maybe only be created when an actual request is made, not as soon as
> Async becomes True. This is obviously even cleaner than assigning Null to
> the references.
>
> @Benoit:
> I would maybe be able to do this myself, if you confirm that we shouldn't be
> calling _free() from Gambas.
>
> Regards,
> Tobi
>

No, you should not call _free(). You should even never have to implement 
it in Gambas code.

The problem is that DnsClient enables its internal watch too early, and 
disables it too late.

It should enable the watch the first time it is actually needed - when 
GetHostName() or GetHostIP() is called - and not when the Async property 
is set.

And it should disable the watch when the async operation is terminated 
or when Stop() is called.

If you can do the change, you are welcome! :-)

Regards,

-- 
Benoît Minisini




More information about the User mailing list