Page 1 of 1

EtherTen Hangs on Ethernet.begin()

Posted: Mon Mar 25, 2013 11:14 am
by ckbuckley
Hey guys,

I've got a sketch for a project I'm working on that needs to pull an IP from a router via DHCP, passing it a MAC from a I2C chip. This all works with no issues if I have it connected to my network when it makes the Ethernet.begin(mac) call, BUT if that fails and it tries to call Ethernet.begin(mac) again (in the loop function, every 30 seconds if there is no local IP), it can only call it twice more before the device hangs completely - so it can only try calling Ethernet.begin(mac) three times in a row before it hangs.

As this device is designed to be started up with no connection, programmed, then connected to the network without having to restart it, this is presenting a bit of a problem.

I've tried modifying EthernetUDP::begin to look like this:

for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i;

In case the cause was a CLOSE_WAIT that's causing the Wiznet to get stuck, but that didn't work. I'm completely out of ideas - am I trying to pull a lease the wrong way in the loop function, or is there a bug in the code somewhere?

Any help would be greatly appreciated :)

Re: EtherTen Hangs on Ethernet.begin()

Posted: Mon Mar 25, 2013 1:05 pm
by ckbuckley
Resolved! Turns out I was close to the answer.

There's a memory leak in the Ethernet library where the _dhcp object instantiated in Ethernet::begin is not garbage collected, and subsequent calls to Ethernet::begin cause a leak of about 100 bytes per call, which was causing the hanging issue after 3-5 calls. A quick patch by Noah Spurrier fixed the issue (check for the existence of a _dhcp object and only instantiate a new one if it doesn't already exist).

Fix is here: ... c=116614.0