Symbian
Symbian OS Library

FAQ-0577 Why does the STDLIB appear to leak memory?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: STDLIB
Created: 04/27/2001 Modified: 09/06/2001
Number: FAQ-0577
Platform: ER5, Symbian OS v6.0

Question:
Why does the STDLIB appear to leak memory?

Answer:
The following code appears to cause a memory leak:
struct stat fileStat;
char* fileName="C:\\test.txt";
__UHEAP_MARK;
stat(fileName, &fileStat);
__UHEAP_MARKEND;

However, this is actually not the case. When many of the stdlib functions are called, memory is allocated for the global variables. This memory is not de-allocated until stdlib is closed, hence the panic when you perform a check as above. To get around this, include stdlib.h and call CloseSTDLIB() before __UHEAP_MARKEND to de-allocate the memory for the buffer used.

Note: The CloseSTDLIB() method will free heap cells allocated heap by the STDLIB. However, it will not close certain handles which STDLIB keeps open. In Symbian OS v6.0 and later CONE includes a check when exiting applications that all handles have been closed (in addition to the normal __UHEAP checks). Applications which use STDLIB will therefore fail this test. The workaround for this is to call CCoeEnv::DisableExitChecks(); under V6.0 and above.