Difference between revisions of "Arduino notes"

From Noah.org
Jump to navigationJump to search
m
Line 17: Line 17:
  
 
Serial.println(freeRam());
 
Serial.println(freeRam());
 +
</pre>
 +
 +
== memory leak in Ethernet.cpp ==
 +
 +
The Ethernet library has a memory leak (for version arduino-1.0.1). Each call to EthernetClass::begin will create a new DhcpClass. The _dhcp member variable is never deleted, so each subsequent call to EthernetClass::begin will leak about 100 bytes. It takes about 10 calls to EthernetClass::begin to cause the Atmega328 to run out of memory. My patch simply checks if _dhcp was already allocated; if allocated then it reuses the previously allocated _dhcp. This may not be the best way to handle this case, but it works in my application and it seems to not cause any trouble in the examples included with the Ethernet library..
 +
 +
<pre>
 +
$ diff -up Ethernet.cpp Ethernet-noah.cpp
 +
--- Ethernet.cpp 2012-03-11 14:30:32.000000000 -0700
 +
+++ Ethernet-noah.cpp 2012-07-30 19:25:24.353578643 -0700
 +
@@ -10,8 +10,9 @@ uint16_t EthernetClass::_server_port[MAX
 +
 +
int EthernetClass::begin(uint8_t *mac_address)
 +
{
 +
-  _dhcp = new DhcpClass();
 +
-
 +
+  if (_dhcp == NULL) {
 +
+    _dhcp = new DhcpClass();
 +
+  }
 +
 +
  // Initialise the basic info
 +
  W5100.init();
 +
@@ -89,6 +90,8 @@ int EthernetClass::maintain(){
 +
        break;
 +
    }
 +
  }
 +
+// else maybe do something if _dhcp is NULL?
 +
+
 +
  return rc;
 +
}
 
</pre>
 
</pre>

Revision as of 19:39, 30 July 2012


debug memory problems (out of RAM)

See Arduino Forum on memory usage. This function will return the amount of RAM free. This accounts for the stack and heap used.

int freeRam () {
  // __brkval is the address of the top of the heap if memory has been allocated.
  // If __brkval is zero then it means malloc has not used any memory yet, so
  // we look at the address of __heap_start.
  extern int __heap_start
  extern int *__brkval; // address of the top of heap
  int stack_top; 
  return (int)&stack_top - ((int)__brkval == 0 ? (int)&__heap_start : (int)__brkval); 
}

Serial.println(freeRam());

memory leak in Ethernet.cpp

The Ethernet library has a memory leak (for version arduino-1.0.1). Each call to EthernetClass::begin will create a new DhcpClass. The _dhcp member variable is never deleted, so each subsequent call to EthernetClass::begin will leak about 100 bytes. It takes about 10 calls to EthernetClass::begin to cause the Atmega328 to run out of memory. My patch simply checks if _dhcp was already allocated; if allocated then it reuses the previously allocated _dhcp. This may not be the best way to handle this case, but it works in my application and it seems to not cause any trouble in the examples included with the Ethernet library..

$ diff -up Ethernet.cpp Ethernet-noah.cpp 
--- Ethernet.cpp	2012-03-11 14:30:32.000000000 -0700
+++ Ethernet-noah.cpp	2012-07-30 19:25:24.353578643 -0700
@@ -10,8 +10,9 @@ uint16_t EthernetClass::_server_port[MAX
 
 int EthernetClass::begin(uint8_t *mac_address)
 {
-  _dhcp = new DhcpClass();
-
+   if (_dhcp == NULL) {
+     _dhcp = new DhcpClass();
+   }
 
   // Initialise the basic info
   W5100.init();
@@ -89,6 +90,8 @@ int EthernetClass::maintain(){
         break;
     }
   }
+// else maybe do something if _dhcp is NULL?
+
   return rc;
 }