Every so often ESP8266 hangs... WifiClient seems to be at fault This code works fine without any freezes:

#include &lt;ESP8266WiFi.h&gt;
#include &lt;Servo.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;ESP8266WiFiMulti.h&gt;

Servo turn;

ESP8266WiFiMulti wifiMulti;

const char* host = "192.168.1.148";
WiFiClient client;

int turnPos = 90;
unsigned long ts = 0;

void setup() {
  startWiFi();
  turn.attach(14);
  turn.write(90);
}

void loop() {

  if (millis() - ts &gt; 500 &amp;&amp; millis() - ts &lt; 1000) {
    turnPos = 160;
  }
  else if (millis() - ts &gt;= 1000) {
    ts = millis();
  }
  else {
    turnPos = 20;
  }


  /*if (client.connected()) {
    }
    else {
    if (!client.connect(host, 3000)) {
    }
    }*/


  turn.write(turnPos);
  yield();
}

void startWiFi() {
  wifiMulti.addAP("DVW3201BE8", "###");   // password is filled in on mny version
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
}


While this code (with the wificlient bit commented out) freezes every so often:

#include &lt;ESP8266WiFi.h&gt;
#include &lt;Servo.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;ESP8266WiFiMulti.h&gt;

Servo turn;

ESP8266WiFiMulti wifiMulti;

const char* host = "192.168.1.148";
WiFiClient client;

int turnPos = 90;
unsigned long ts = 0;

void setup() {
  startWiFi();
  turn.attach(14);
  turn.write(90);
}

void loop() {

  if (millis() - ts &gt; 500 &amp;&amp; millis() - ts &lt; 1000) {
    turnPos = 160;
  }
  else if (millis() - ts &gt;= 1000) {
    ts = millis();
  }
  else {
    turnPos = 20;
  }


  if (client.connected()) {
    }
    else {
    if (!client.connect(host, 3000)) {
    }
    }


  turn.write(turnPos);
  yield();
}

void startWiFi() {
  wifiMulti.addAP("DVW3201BE8", "###");   // password is filled in on mny version
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
}


Only difference is in the working one the WifiClient bit of code is commented out.   How would WifiClient cause this problem?
It is a bug and it was fixed in Arduino esp8266 core package 2.4.2 published 2 days ago.

see first line in ESP8266WiFi section of the changes list


  2.4.2 ESP8266WiFi:
  Fix memory leak when reusing WiFiClient object (#4497, #4516, #4549)