Exception when creating array of ON_LineCurve


I have an odd issue here: when I run the following code

ON_SimpleArray<ON_LineCurve> linesArray;

ON_LineCurve lineA(ON_3dPoint(0, 0, 0), ON_3dPoint(0, 0, 1));
ON_LineCurve lineB(ON_3dPoint(0, 0, 1), ON_3dPoint(0, 0, 2));


I get an exception

Exception thrown at 0x00007FF8CCF92D5A (opennurbs.dll) in Rhino.exe: 0xC0000005: Access violation reading location 0x00000000000000B0

What’s happening? I need to create an object of kind ON_SimpleArray<ON_LineCurve> to track an array of lines.

Many thanks,

Hi @pagarcia,

There are three different template arrays in openNURBS. This from opennurbs_array.h:

ON_SimpleArray - This template is more efficient than the ON_ClassArray<> template, but it should not be used for arrays of classes that require explicit construction, destruction, or copy operators.

ON_ClassArray - This template is designed to be used with classes that require non-trivial construction or destruction. Any class used with this template must have a robust operator=().

ON_ObjectArray - This array is used to store lists of classes that are derived from ON_Object. It differs from ON_ClassArray in that the virtual ON_Object::MemoryRelocate function is called when growing the dynamic array requires changing the location of the memory buffer used to store the elements in the array.

Given this, you can either do this:

ON_SimpleArray<ON_LineCurve*> lines0;
ON_LineCurve* p0 = new ON_LineCurve(ON_3dPoint(0, 0, 0), ON_3dPoint(0, 0, 1));
ON_LineCurve* p1 = new ON_LineCurve(ON_3dPoint(0, 0, 1), ON_3dPoint(0, 0, 2));

If you do, then don’t forget to recover the allocated memory.

for (int i = 0; i < lines0.Count(); i++)
  if (nullptr != lines0[i])
    delete lines0[i];
    lines0[i] = nullptr;

or you can do this:

ON_ObjectArray<ON_LineCurve> lines1;
ON_LineCurve l0(ON_3dPoint(0, 0, 0), ON_3dPoint(0, 0, 1));
ON_LineCurve l1(ON_3dPoint(0, 0, 1), ON_3dPoint(0, 0, 2));

Hope this helps.

– Dale

1 Like

Thank you for the reply @dale, it’s very clear now.