Segfault on TransferRoots when trying to read consecutive STEP files using STEPControl_Reader

I'm trying to read in multiple STEP files (two in this case) and add them to an assembly shape. This function reads the file:

void OccView::readModel(QString filename, TopoDS_Compound &shape) {

  TopoDS_Shape result;

  QFileInfo file(filename);

  if (file.suffix() == QString("step") ||
      file.suffix() == QString("stp")) // STEP File
  {
    STEPControl_Reader reader;
    if (!Interface_Static::SetCVal("xstep.cascade.unit", "M"))
      RCLCPP_ERROR(rclcpp::get_logger("OpenCASCADE"),
                   "Error setting xstep.cascade.unit");
    reader.ReadFile(filename.toStdString().c_str());
    reader.TransferRoots();
    result = reader.OneShape();
  } else // IGES File
  {
    IGESControl_Reader reader;
    if (!Interface_Static::SetCVal("xstep.cascade.unit", "M"))
      RCLCPP_ERROR(rclcpp::get_logger("OpenCASCADE"),
                   "Error setting xstep.cascade.unit");
    reader.ReadFile(filename.toStdString().c_str());
    Handle(TColStd_HSequenceOfTransient) list = reader.GiveList("iges-faces");
    reader.TransferList(list);
    result = reader.OneShape();
  }
  ShapeUpgrade_UnifySameDomain usd(result);
  usd.Build();
  TopoDS_Builder builder;
  builder.MakeCompound(shape);
  builder.Add(shape, usd.Shape());
  TopExp_Explorer explorer(shape, TopAbs_EDGE);
  while (explorer.More()) {
    BRepAdaptor_Curve curv(TopoDS::Edge(explorer.Current()));
    if (curv.GetType() == GeomAbs_Circle && curv.IsClosed() == true) {
      gp_Circ circle = curv.Circle();
      TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex(circle.Location());
      builder.Add(shape, vertex);
    }
    explorer.Next();
  }
}

This function then loops through a list of filenames and adds each shape to a TopoDS_Compound:

void OccView::loadModels(
    std::vector<std::pair<QString, Quantity_Color>> items) {

  TopoDS_Compound assembly;

  TopoDS_Builder builder;
  builder.MakeCompound(assembly);

  for (auto i : items) {
    TopoDS_Compound shape;
    OccView::readModel(i.first, shape);
    myColors->SetColor(shape, i.second, XCAFDoc_ColorGen);
    builder.Add(assembly, shape);
  }

  OccView::displayShape(assembly);
}

However, I get a segfault on reader.TransferRoots() for the second file (always the second, both load fine individually, and swapping them around makes no difference).

Here are the relevant lines of the stack trace:

libTKXSBase.so.7.8!Interface_InterfaceModel::NbEntities() const (Unknown Source:0)
libTKDESTEP.so.7.8!STEPControl_Reader::NbRootsForTransfer() (Unknown Source:0)
libTKXSBase.so.7.8!XSControl_Reader::TransferRoots(Message_ProgressRange const&) (Unknown Source:0)
ampi_project_ui::OccView::readModel(ampi_project_ui::OccView * const this, ampi_project_ui::OccView * const this@entry, QString filename, TopoDS_Compound & shape) (/home/ralphieraccoon/ros2_ws/src/ampi_project_ui/src/OccView.cpp:345)

I'm running 7.8 on Ubuntu 24.04.1.

Dmitrii Pasukhin's picture

 Hello. The issue is not clear at all. I'm using Debian 12 and Ubuntu 24 on daily work. But I never get that issue.

Which complier did you use for OCCT build? And CMake settings. I will try to reproduce.

It is a default 7.8 or 7.8.1 or some specific commit inside master?

Best regards, Dmitrii.

Damian Crosby's picture

Hi Dimitrii,

I am using a prebuilt library from https://launchpad.net/ubuntu/+source/opencascade

It is version 7.8.1 according to dpkg (I'm using a bleedingedge ppa to get a later version, I needed it for some of the BRepTools.write() options).

Dmitrii Pasukhin's picture

Thank you. I will try to check.

Could you share which options? I hope we can somehow improve user experience with that issue.

Best regards, Dmitrii.

Damian Crosby's picture

It's the options to not include triangulation and normal data in the export, so the file is a lot smaller. I believe it's in some older versions too, but not the version normally installed with 24.04.1.