Questions on Memory Management in OpenCascade

Hi,
I have a few questions about memory management in OCCT.
I have read the documentation in: 
https://www.opencascade.com/doc/occt-7.0.0/overview/html/occt_user_guide...
and looked at the source code for the samples.

As I understand it, OCCT has Handles/smartpointers , to simplify memory management.
For instance in the sample program ImportExport, (in CImportExportDoc::OnFileImportStep() ) there is the line:
    Handle(TopTools_HSequenceOfShape) aSeqOfShape = CImportExport::ReadSTEP();
This aSeqOfShape is not explicitly deleted anywhere, rather it has a reference count which is decremented when  aSeqOfShape goes out of scope, causing aSeqOfShape and all its contents to be deleted.
Is this correct?       

To use these handles, is it necessary to have the  environment variable "MMGT_OPT=1 set?

Working with this "Handle(TopTools_HSequenceOfShape) aSeqOfShape" ,
When I  do aSeqOfShape.Remove() (or Clear() ) does OCCT delete the object(s) removed from the TopTools_HSequenceOfShape? 

What about TopTools_HSequenceOfShape's  SetValue()?   
If I do: 
    aSeqOfShape.SetValue(1, aShape); // where aShape is a  TopoDS_Shape
Does the existing TopoDS_Shape at index 1 in the TopTools_HSequenceOfShape get deleted , and replace by  aShape ?

I am also using a NCollection_DataMap, I have:
    NCollection_DataMap<TopoDS_Shape, TopTools_HSequenceOfShape> m_Faces;
This is map , where the keys are TopoDS_Shape, and the values are a TopTools_HSequenceOfShape.
(I am using this to store the TopoDS_Faces of the TopoDS_Shape in  TopTools_HSequenceOfShape).
If I do 
    m_Faces.Clear();
Does this delete all the contents of m_Faces, i.e all the TopoDS_Shapes, and all the contents of all the TopTools_HSequenceOfShapes?

Thank you very much

Jim

Roman Lygin's picture

Jim,
 

Handles is just OCC own implementation of a concept of a smart pointer. Refer to std::shared_ptr and boost::intrusive_ptr if you are unfamiliar with the concept.

No, you do not need to set MMGT_OPT to 1 to use handles. They are invariant to MMGT_OPT value. The latter defines which memory manager (a low-level provider of allocation/deallocation/reallocation of memory chunks) is used.

All (most) OCC classes redefine new/delete operator to use Standard::Allocate()/::Free() functions for memory allocation. Underneath this API dispatches which memory manager the request goes to:
- OS/C++ run-time (MMGT_OPT=0)
- [deprecated?] OCC manager (MMGT_OPT=1)
- TBB (MMGT_OPT=2)

If MMGT_OPT is undefined then the choice is determined by a value set by OCCT_MMGT_OPT_DEFAULT macro in compile time (0 by default).

Destruction of all containers, handles, etc is done automatically, you don't have to take care of that. Of course, you need to make sure no cyclic dependencies are between the handles or take care of breaking such dependencies. All this is consistent with smart pointers and containers defined in C++ STL.

Hope this helps.

Roman
 

Jim Williams's picture

Thanks for your helpful and informative reply, Roman. 

Jim