Memory leaks/crashes when using std::vector<TopoDS_Shape>

I'm using OCC 6.6.0; I build up a vector of TopoDS_Shape objects from the IGES reader class, meshing any TopoDS_Faces, and then store each individual object in a separate wrapper object. In each case I pass and store the TopoDS_Shape by value, so I would not expect there to be any memory shared. However, if I delete the original vector I built up, then I get an access violation exception when I later delete my wrapper object; if I reverse the order and refrain from deleting the vector until the wrapper object, then I get an error as soon as I delete the vector.

Is TopoDS_Face somehow sharing memory between instances? If so, is there a way to either suppress this behavior or otherwise fix my issue? Currently I am not deleting the vector, but this is causing a very bad memory leak. It seems that no matter when I try to remove data from the vector or delete it, it causes an issue.

Laszlo Kudela's picture

Hello Sam,

TopoDS_Shape and its children contain a handle to a TopoDS_TShape. I think that if you pass around TopoDS_Shapes by value, this pointer location can get messed up.

It is a good approach to use the container classes provided by OpenCASCADE instead of using std::vector. Have a look at TopTools_Array1OfShape or TopTools_ListOfShape.

I hope this more or less answers your question.

László

Sam Shrum's picture

Hi Laszlo,

That certainly confirms some of my suspicions, but I'm not sure how to ensure that a TopoDS_Shape never gets passed by value. When reading the shapes out of an XSControl_Reader class, they are returned by value from the internal collection. However, even if I pass this initial object only by reference or pointer, the reference will be invalidated once that stack frame expires, correct? I've just converted my program to use TopTools_ListOfShape everywhere that I used std::vector, and to store the final objects in references rather than values. However, on my first attempt at accessing the reference, I get a crash because the reference is no longer valid.

Billy's picture

Also if you read coding rules pdf of OCC 6.7.0, since objects may have handles to other objects, you should use .Nullify(); method to free memory and not delete.

Sam Shrum's picture

Where is said PDF located? Is there an equivalent for 6.6.0?

Sam Shrum's picture

I resolved the issue; during a hunt for memory leaks I had eliminated part of the triangulation object, which caused a double deletion when I later went to fix the memory leak of the faces themselves not being eliminated. Using std::vector didn't cause any issues after that.