Quit Bugging Me: Memory Leak!

A perrenial question arrises… "Did I find a memory leak in the shell?  See what it does?"

This question naturally arises after a customer has been debugging a while, and runs short on memory for some allocation.  They check into it, and they find that when they call certain shell functions, they find memory missing after the call.

Here are typical examples of "memory leaks" from the shell…

ping ""
lkup "myVariableName"
hostAdd "hostname", ""
printf ("This is a string: %s\n", "no it isn't")
Though these really look like memory leaks – accidental allocations without free's, these are not memory leaks.  What is happening is the shell has to allocate space for the strings you're handing to it, so when it parses the statements, it replaces the character strings with (essentially) pointers-to-characters (char *).   Using memShow before and after each of the above shell commands you will see memory has been allocated and not returned to the system.  "AHA!  A Memory leak!!"  Well, no… here's how to show the leak isn't a real "leak:  use a variable.  For example, the following sequences will show that the memory is used when allocating space for the strings:
addrs = ""
myVar = "myVariableName"
hostname = "hostname"
format = "This is a string: %s\n"
argString = "no it isn't"
ping addrs
lkup myVar
hostAdd hostname, addrs
In the course of adding the new host name, you may have added an entry to the hosts-table, and may therefore have used some space – but note that the other commands no longer consume memory.  By allocating space for the strings before hand, you can detect the real amount of memory used by the function calls.  (in this case, I'll leave the target-side executions to the interested readers…)