netgen/libsrc/geom2d/geom2dpkg.cpp
Monty Montgomery de7ffc5906 Eliminate a "C++ initialization order fiasco" for geometryregister
Current initialization of the global geometryregister suffers from a
classic 'initialization order fiasco'.  Depending on the order the
compilation units are loaded/linked, the initialization of the global
geometryregisterarray is not guaranteed to happen (and indeed often
does not happen) before it is used.  This leads to entries being
appended before it's initialized (usually 'suceeding, but potentially
causing memory corruption if the segment at that point isn't zeroed),
initialization then happening halfway through (wiping the initial
entries) and then the last entries being the only ones that show up.

The net effect is either a crash at startup, or several geometry types
seeming to be missing.  Eg, step files will oad, but STL files are
just ignored.  The bug is actively observed on, eg, Linux.

This patch implements a simple 'initialize at first access' convention
for the array, eliminating the ordering problem.

I've not reviewed the rest of the source for other potential examples
of the fiasco pattern; this fixes only the geometryregister, since
that was actively biting.
2022-05-22 11:29:10 -04:00

57 lines
1.2 KiB
C++

#include <meshing.hpp>
#include <geometry2d.hpp>
#include <visual.hpp>
#include <inctcl.hpp>
#include "vsgeom2d.hpp"
// extern "C" int Ng_CSG_Init (Tcl_Interp * interp);
namespace netgen
{
// extern DLL_HEADER NetgenGeometry * ng_geometry;
static VisualSceneGeometry2d vsgeom2d;
class SplineGeometryVisRegister : public GeometryRegister
{
public:
virtual NetgenGeometry * Load (const filesystem::path & filename) const { return NULL; }
virtual VisualScene * GetVisualScene (const NetgenGeometry * geom) const;
};
VisualScene * SplineGeometryVisRegister :: GetVisualScene (const NetgenGeometry * geom) const
{
const SplineGeometry2d * geometry = dynamic_cast<const SplineGeometry2d*> (geom);
if (geometry)
{
vsgeom2d.SetGeometry (geometry);
return &vsgeom2d;
}
return NULL;
}
}
using namespace netgen;
#ifdef WIN32
extern "C" __declspec(dllexport) int Ng_geom2d_Init (Tcl_Interp * interp);
#else
extern "C" int Ng_geom2d_Init (Tcl_Interp * interp);
#endif
int Ng_geom2d_Init (Tcl_Interp * interp)
{
GeometryRegisterArray& gra = FetchGeometryRegisterArray();
gra.Append (new SplineGeometryVisRegister);
return TCL_OK;
}