Fixing ESP8266mDNS MDNS.queryService() Crashes

I ran into some very frustrating, mysterious errors wile trying to use MDNS.queryService()  in the ESP8266 Arduino SDK. What should have been a simple call to attempt to resolve an mDNS name turned into an exception and stack dump.

Code that looked like this:

led to this:

Turned out there’s a simple solution to this problem.

I’d forgotten to call MDNS.begin()  first. This prepares the MDNS class for further calls, and the class isn’t written to detect the fact that begin()  wasn’t called already. Once I made sure to call  MDNS.begin() before calling  MDNS.queryService() the problem went away and the code worked properly.