[Bug report] seg fault on access of curve representation of poles on sphere

the following code does nothing but creating a simple sphere and traversing its edge representation curves, accessing the connected Geom curve objects. I get a segmentation fault! The 3d curve representation object for the poles (at least one of them) seems to be incorrectly initialized. I use the _OCC64 compiler flag and haven't had any other "mysterious" crashes yet.

TopoDS_Face sphere = BRepBuilderAPI_MakeFace(gp_Sphere(gp_Ax3(gp_Pnt(0,0,0), gp_Dir(0,1,0)), 1.0));
TopExp_Explorer Ex;
for (Ex.Init(sphere,TopAbs_EDGE); Ex.More(); Ex.Next())
{
TopoDS_Edge edge = TopoDS::Edge(Ex.Current());
Handle(BRep_TEdge) hTEdge = Handle(BRep_TEdge)::DownCast(edge.TShape());
BRep_ListOfCurveRepresentation const &repr = hTEdge->Curves();
BRep_ListIteratorOfListOfCurveRepresentation iter(repr);
for(;iter.More();iter.Next())
{
Handle(BRep_CurveRepresentation) rep = iter.Value();
if(rep->IsCurve3D())
{
Handle(Geom_Curve) curve = rep->Curve3D();
curve->DynamicType();
}
}
}

Can anyone reproduce the crash?

sergey zaritchny's picture

Hi Fabian,
Could you give a little bit more details important for crash reproducing:
1. Operation system, platform
2. Compiler
3. OCCT version
Thanks

Fabian Hachenberg's picture

sorry,

1. SUSE Linux Enterprise Desktop 10 SP2, x86_64
2. gcc 4.1.2 20070115
3. OCC 6.3.0

sergey zaritchny's picture

Hi Fabian,
Thanks for details.
Yes,your code raises the exception. But it is not a bug.
It seems you try to get curve representation from degenerated edge.
Regards

Fabian Hachenberg's picture

well in my opinion I shouldn't be able to retrieve a representation object which is uninitialized and leads to a seg fault on access. Is there at least a note in the reference documentation that accessing curve representations of degenerated curves can lead to segmentation faults? At least for me this is light years away from being obvious.

EricThompson's picture

Fabian-

The representation object is not uninitialized. It is initialized to NULL. Before dereferencing rep you should check that it is not null:

if( !rep.IsNull() )
{
if(rep->IsCurve3D())
...

This should be your standard practice before dereferencing any handle returned to you.

Note: I believe that the apex of a cone is also a degenerate curve, and they can occur for certain toruses too.

Fabian Hachenberg's picture

thank you, I will adopt this procedure in the future :-)

sergey zaritchny's picture

Hi,
I would added several words to clarify this topic.
For NULL must always be checked not only a curve representation itself,
but a Curve3D too:
if (!CR->Curve3D().IsNull()) {...}
It is a feature of OCCT.

Regards