Hello Bernard,
as you might know, Open CASCADE implements its own memory management mechanism adapted to the particular needs
of the classes which use it. Our experience shows that sometimes behavior of the memory management may not be quite understandable by the end users, partially due to its complexity, partially due to lack of documentation.
It seems to us that, in addition to the improvements in documentation, we could think of creating some sort of FAQ
for such type of subjects. In this message, I just give some brief recommendations.
In order to isolate memory leaks in the program based on Open Cascade, I recommend you to use Rational Purify (licensed tool of IBM Rational Software) instead of VC++ embedded tools. It gives much more help, because it makes exhaustive analysis of all allocated memory blocks and reports only those to which no pointer exists anywhere in the application space. In addition, it reports the stack of the application at the moment of allocation of each leaked block.
But even in this case great amount of memory is false detected and reported as leaked. For that there is a simple reason: some OCC functions do not store/return the pointer to the original block that malloc returns, but rather add or subtract an offset value and store/return the result of offset. In
this case really there is no leak, because there is an opposite action before releasing memory: the same offset is subtracted/added from a pointer. But Purify reports all blocks allocated (and not yet freed) by these functions. For example, such behavior is implemented in the functions
Standard::MAllocate and Standard::MFree, in the generic classes TCollection_Array1 and TCollection_Array2, in the functions cmn_getmem and cmn_freemem in the package OpenGl.
In order to reduce the number of false detected leaks you can set the environment variable MMGT_OPT to 0 before OCC application launch. It switches off optimization code in OCC allocator (package Standard) so that the allocator returns exactly the same pointer that returns malloc.
Using the filtering technique provided by Purify, it is easy to filter out messages concerning Arrays and OpenGl.
Some efforts were done internally in OCC to eliminate memory leaks in the toolkits TKernel and TKCDF using the described technique. Several real leaks were found in TCollection strings and in LDOM package, and the patch comes with the last OCC maintenance release which is available for our customers only. All other users may expect it to appear in the following public version of Open CASCADE.
With best regards,
Commenter-3