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

Tobias Boege taboege at ...626...
Wed Jul 23 23:12:09 CEST 2014


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

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list