Hardware Reference
In-Depth Information
void* buffer = malloc(2);
for (int i = 4; i <= 8; i+=2) {
buffer = realloc(buffer, i);
}
free(buffer);
ATS_PrintTestStatus("Reallocating repeatedly larger preserves free memory",
startMemory == ATS_GetFreeMemory());
}
void loop() {
}
Example: Testing for a Memory Leak
Any new values created inside the memory test will use memory. So, you must declare all the variables that consume
memory at the beginning of the setup.
startMemoryUsage = ATS_GetFreeMemory();
Once this is done, your starting memory is set. Anything that takes without putting back will be counted as a
failure. The memory test is over when you call the following:
ATS_ReportMemoryUsage(startMemoryUsage);
Here are some hints for debugging:
By putting the memory test at the bottom of the code, you can gradually move it higher into
the code and see where the memory was lost.
An
OK indicates that the memory loss occurred below the memory test.
A binary search will help you find the problem.
Listing 13-7 is a sketch of the testing skeleton.
Listing 13-7. Sketch of the Testing Skeleton
#include <ArduinoTestSuite.h>
//************************************************************************
void setup()
{
int startMemoryUsage;
//startMemoryUsage must be set directly before ATS_begin
startMemoryUsage = ATS_GetFreeMemory();
ATS_begin("Arduino", "Skeleton Test");
/*
* Test Run Start
* Test one passes because result is set to true
* Test two fails becuase result is set to false
* You can test memory for any set of tests by using the ATS_ReportMemoryUsage test
 
Search WWH ::




Custom Search