Memory Leaks with BRepMesh

Does anyone konw if there are known memory leaks with the OCC Mesh algorithms. The following code generates a steady consumption of memory which is only released when the application closes

for(int i=0;i {
BRepBuilderAPI_MakePolygon w(gp_Pnt(0,0,0),gp_Pnt(0,100,0),gp_Pnt(20,100,0),gp_Pnt(20,0,0));
TopoDS_Wire wireShape( w.Wire());
BRepBuilderAPI_MakeFace faceBuilder(wireShape);
TopoDS_Face f( faceBuilder.Face());
BRepMesh_IncrementalMesh im(f,1);


If you comment out the BRepMesh_IncrementalMesh line the memory consumtion flatlines. BRepTools::Clean seems to have no impact whatsoever on memory usage.
I am a real fan of OCC but this as I only really need mesh generation from a BRep model and I need to do this a lot of times, this problem is show stopper for me.
I have tried working through the source and wonder if it may be some form of cyclic referencing that is stopping the OCC memory manager from cleaning up. Does anyone have any ideas please?

Steve Lockley's picture

OK, I think I have found the cause of the leak, it appears to be in

void BRepMesh_FastDiscret::Add(const TopoDS_Face& theface)

at the line

BRepMesh_Classifier* classifier = new BRepMesh_Classifier(face, tolclass, internaledges, myvemap, structure, myumin, myumax, myvmin, myvmax);

The classifier pointer does not appear to be deleted, if a delete operator is added at the end of the classifier's scope then the memory leak dissappears.

My understanding of OCC is not good enough to let me know if this is a sensible fix, I assume there was a good reason to create a pointer to a BRepMesh_Classifier in this way, maybe it is passed out and used somewhere else.

I would really appreciate someone with a strong knowledge of OCC to help me know if this might be the solution to the memory leak problem that I am observing

Bearloga's picture

Hi Steve,
I think you have found and fixed a real bug, as nowhere this pointer is passed in the code outside of this class.

Steve Lockley's picture

I'm new to the OpenCascade forum, if this is a bug how do we notify OCT or do they just pick it up from here?

Bearloga's picture

Nobody knows how they do, but some fixes posted here earlier are already in the source code.

Jeanmi B's picture

Hi Steve,

the classifier seems to be deleted a few lines below (line 496 in BRepMesh_FastDiscret)

if (myfacestate != BRepMesh_NoError && myfacestate != BRepMesh_ReMesh)
delete classifier;

You should trace in debugger to see whether or not the classifier is deleted before end of the function.


Forum supervisor's picture

Dear Mr. Lockley,

Thank you for reporting this problem in Open CASCADE Technology and your deep investigation!

We are pleased to inform you that the problem you reported has been registered as bug with reference number OCC20627 "Memory leaks in BRepMesh".

Please note that you will be able to track fixing on that bug by consulting the Release Notes of Open CASCADE Technology that are published on our web site at

Yours sincerely,
Open CASCADE Support Team