segfault with triangle.refine
Dear @inducer
I encounter a segfault again when calling triangle.refine to refine a generated mesh (maybe, it is my compiler's problem ?)
The codes are posted below. A short description of the codes, I generated mesh from a circle (a small square is within the circle) and I want to further refine the already generated mesh by imposing a more strict refinement_func, and the gdb debug information is posted after the code.
BTW, can i pass more parameters to the refinement_func ? for example, the polygon in refinement_func_square, so that I could write more compact refinement functions for different regions of the mesh.
Best!
from __future__ import division
import meshpy.triangle as triangle
import numpy as np
from matplotlib.path import Path
def round_trip_connect(start, end):
return [(i, i+1) for i in range(start, end)] + [(end, start)]
def needs_refinement(tri_points, area):
return bool(area > 0.1)
def needs_refinement2(tri_points, area):
return bool(area > 0.05)
def needs_refinement_square(tri_points, area):
points = [(1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1), (1, 0)]
psquare = Path(points)
center_tri = np.sum(np.array(tri_points), axis=0)/3.
if psquare.contains_point(center_tri):
if bool(area > 0.05):
return True
elif area>0.1:
return True
else:
return False
def main():
points = [(1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1), (1, 0)]
facets = round_trip_connect(0, len(points)-1)
circ_start = len(points)
points.extend(
(3 * np.cos(angle), 3 * np.sin(angle))
for angle in np.linspace(0, 2*np.pi, 30, endpoint=False))
facets.extend(round_trip_connect(circ_start, len(points)-1))
markers = [2,2,2,2,2,2]
markers.extend(list(np.ones(30, dtype='int')))
markers = [int(i) for i in markers]
info = triangle.MeshInfo()
info.set_points(points)
#info.set_holes([(0, 0)])
info.set_facets(facets, facet_markers=markers)
mesh = triangle.build(info, refinement_func=needs_refinement)
mesh = triangle.refine(mesh, refinement_func=needs_refinement2)
mesh_points = np.array(mesh.points)
mesh_tris = np.array(mesh.elements)
mesh_attr = np.array(mesh.point_markers)
print(mesh_attr)
import matplotlib.pyplot as plt
plt.triplot(mesh_points[:, 0], mesh_points[:, 1], mesh_tris)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
#
fig = plt.gcf()
fig.set_size_inches(4.2, 4.2)
#plt.savefig('../../figs/sec5-meshpy-triangle-ex4.pdf')
if __name__ == "__main__":
main()
(gdb) file python
Reading symbols from python...(no debugging symbols found)...done.
(gdb) run triangle-ex4.py
Starting program: /usr/bin/python triangle-ex4.py
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff60efcdc in reconstruct () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
(gdb) bt
#0 0x00007ffff60efcdc in reconstruct () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#1 0x00007ffff60f4e5b in triangulate () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#2 0x00007ffff60d0602 in triangulateWrapper(char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*) () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#3 0x00007ffff60d8ec7 in meshpyboost::python::objects::caller_py_function_impl<meshpyboost::python::detail::caller<void (*)(char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*), meshpyboost::python::default_call_policies, meshpyboost::mpl::vector6<void, char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*> > >::operator()(_object*, _object*) ()
from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#4 0x00007ffff60ff552 in meshpyboost::python::objects::function::call(_object*, _object*) const () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#5 0x00007ffff60ff657 in meshpyboost::detail::function::void_function_ref_invoker0<meshpyboost::python::objects::(anonymous namespace)::bind_return, void>::invoke(meshpyboost::detail::function::function_buffer&)
() from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#6 0x00007ffff610cf0d in meshpyboost::python::handle_exception_impl(meshpyboost::function0<void>) () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#7 0x00007ffff60fce7f in function_call () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#8 0x00007ffff79a6ff8 in PyObject_Call (func=func@entry=0x721cf0, arg=arg@entry=0x7ffff7ef6990, kw=kw@entry=0x0) at Objects/abstract.c:2067
#9 0x00007ffff7a57393 in do_call (nk=<optimized out>, na=5, pp_stack=0x7fffffffdd30, func=<optimized out>) at Python/ceval.c:4463
#10 call_function (oparg=<optimized out>, pp_stack=0x7fffffffdd30) at Python/ceval.c:4261
#11 PyEval_EvalFrameEx (f=0x7ffff010f048, throwflag=<optimized out>) at Python/ceval.c:2836
#12 0x00007ffff7a5d859 in PyEval_EvalCodeEx (_co=0x7ffffffe9670, _co@entry=0x7ffff7e4a660, globals=0x7ffff7e48db0, locals=0x7ffff010f1c0, args=0x7ffff7eaadc8, argcount=170096, argcount@entry=1, kws=0x29fe0,
kwcount=0, defs=0x7ffff7e496f0, defcount=4, kwdefs=0x0, closure=0x0) at Python/ceval.c:3585
#13 0x00007ffff7a5b95f in fast_function (nk=1, na=1, n=<optimized out>, pp_stack=0x7fffffffdf30, func=<optimized out>) at Python/ceval.c:4341
#14 call_function (oparg=<optimized out>, pp_stack=0x7fffffffdf30) at Python/ceval.c:4259
#15 PyEval_EvalFrameEx (f=f@entry=0xc0b0d8, throwflag=throwflag@entry=0) at Python/ceval.c:2836
#16 0x00007ffff7a5c2b2 in fast_function (nk=<optimized out>, na=<optimized out>, n=0, pp_stack=0x7fffffffe050, func=<optimized out>) at Python/ceval.c:4331
#17 call_function (oparg=<optimized out>, pp_stack=0x7fffffffe050) at Python/ceval.c:4259
#18 PyEval_EvalFrameEx (f=0x7ffff7f45438, throwflag=<optimized out>) at Python/ceval.c:2836
#19 0x00007ffff7a5d859 in PyEval_EvalCodeEx (_co=0x7ffffffe9670, _co@entry=0x7ffff7ed6c00, globals=0x0, locals=0x7ffff7f455b0, args=0x0, argcount=170096, argcount@entry=0, kws=0x29fe0, kws@entry=0x0, kwcount=0,
defs=0x0, defcount=0, kwdefs=0x0, closure=0x0) at Python/ceval.c:3585
#20 0x00007ffff7a5d8fb in PyEval_EvalCode (co=co@entry=0x7ffff7ed6c00, globals=globals@entry=0x7ffff7f42508, locals=locals@entry=0x7ffff7f42508) at Python/ceval.c:773
#21 0x00007ffff7a79bb4 in run_mod (mod=mod@entry=0x6fb3a8, filename=filename@entry=0x7ffff7e489b0, globals=globals@entry=0x7ffff7f42508, locals=locals@entry=0x7ffff7f42508, flags=flags@entry=0x7fffffffe2d0,
arena=arena@entry=0x6c8fe0) at Python/pythonrun.c:2180
#22 0x00007ffff7a7bde5 in PyRun_FileExFlags (fp=0x677850, filename_str=<optimized out>, start=<optimized out>, globals=0x7ffff7f42508, locals=0x7ffff7f42508, closeit=1, flags=0x7fffffffe2d0)
at Python/pythonrun.c:2133
#23 0x00007ffff7a7cdb3 in PyRun_SimpleFileExFlags (fp=0x7ffffffe9670, filename=0x7ffff7f393b0 "triangle-ex4.py", closeit=1, flags=0xc38780) at Python/pythonrun.c:1606
#24 0x00007ffff7a9301c in run_file (p_cf=<optimized out>, filename=<optimized out>, fp=<optimized out>) at Modules/main.c:319
#25 Py_Main (argc=-136017680, argv=0x7ffff7f39390) at Modules/main.c:751
#26 0x0000000000400af6 in main ()
(gdb)
For some reason, triangle thinks that a list of maximum areas has been specified. (as indicated by behavior.vararea flag). But trianglearealist is NULL, and so the access below crashes.
>│11372 area = trianglearealist[elementnumber - b->firstnumber];
I'd investigate why that flag is set.
a minimal example to reproduce the core dump,
from __future__ import division
import meshpy.triangle as triangle
import numpy as np
def round_trip_connect(start, end):
return [(i, i+1) for i in range(start, end)] + [(end, start)]
def main():
points = [(1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1), (1, 0)]
facets = round_trip_connect(0, len(points)-1)
# build
info = triangle.MeshInfo()
info.set_points(points)
info.set_facets(facets)
#
mesh = triangle.build(info, max_volume=0.1)
mesh = triangle.refine(mesh)
if __name__ == "__main__":
main()
and the gdb outputs
(gdb) run triangle-ex1-naive-refine.py
Starting program: /usr/bin/python triangle-ex1-naive-refine.py
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff60efcdc in reconstruct () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
(gdb) bt
#0 0x00007ffff60efcdc in reconstruct () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#1 0x00007ffff60f4e5b in triangulate () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#2 0x00007ffff60d0602 in triangulateWrapper(char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*) () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#3 0x00007ffff60d8ec7 in meshpyboost::python::objects::caller_py_function_impl<meshpyboost::python::detail::caller<void (*)(char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*), meshpyboost::python::default_call_policies, meshpyboost::mpl::vector6<void, char*, tMeshInfo&, tMeshInfo&, tMeshInfo&, _object*> > >::operator()(_object*, _object*) ()
from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#4 0x00007ffff60ff552 in meshpyboost::python::objects::function::call(_object*, _object*) const () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#5 0x00007ffff60ff657 in meshpyboost::detail::function::void_function_ref_invoker0<meshpyboost::python::objects::(anonymous namespace)::bind_return, void>::invoke(meshpyboost::detail::function::function_buffer&)
() from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#6 0x00007ffff610cf0d in meshpyboost::python::handle_exception_impl(meshpyboost::function0<void>) () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#7 0x00007ffff60fce7f in function_call () from /usr/lib/python3.4/site-packages/MeshPy-2014.1-py3.4-linux-x86_64.egg/meshpy/_triangle.cpython-34m.so
#8 0x00007ffff79a6ff8 in PyObject_Call (func=func@entry=0x7413c0, arg=arg@entry=0x7ffff7f62990, kw=kw@entry=0x0) at Objects/abstract.c:2067
#9 0x00007ffff7a57393 in do_call (nk=<optimized out>, na=5, pp_stack=0x7fffffffdd20, func=<optimized out>) at Python/ceval.c:4463
#10 call_function (oparg=<optimized out>, pp_stack=0x7fffffffdd20) at Python/ceval.c:4261
#11 PyEval_EvalFrameEx (f=0x7ffff7e5c448, throwflag=<optimized out>) at Python/ceval.c:2836
#12 0x00007ffff7a5d859 in PyEval_EvalCodeEx (_co=0x7ffffffe9660, _co@entry=0x7ffff7e5f270, globals=0x1, locals=0x7ffff7e5c5c0, args=0x7ffff7e5cbe8, argcount=10898920, argcount@entry=1, kws=0xcf30, kwcount=0,
defs=0x7ffff65c08d0, defcount=4, kwdefs=0x0, closure=0x0) at Python/ceval.c:3585
#13 0x00007ffff7a5b95f in fast_function (nk=0, na=1, n=<optimized out>, pp_stack=0x7fffffffdf20, func=<optimized out>) at Python/ceval.c:4341
#14 call_function (oparg=<optimized out>, pp_stack=0x7fffffffdf20) at Python/ceval.c:4259
#15 PyEval_EvalFrameEx (f=f@entry=0x7ffff7e5ca48, throwflag=throwflag@entry=0) at Python/ceval.c:2836
#16 0x00007ffff7a5c2b2 in fast_function (nk=<optimized out>, na=<optimized out>, n=0, pp_stack=0x7fffffffe040, func=<optimized out>) at Python/ceval.c:4331
#17 call_function (oparg=<optimized out>, pp_stack=0x7fffffffe040) at Python/ceval.c:4259
#18 PyEval_EvalFrameEx (f=0x7ffff7f45438, throwflag=<optimized out>) at Python/ceval.c:2836
#19 0x00007ffff7a5d859 in PyEval_EvalCodeEx (_co=0x7ffffffe9660, _co@entry=0x7ffff7ed6150, globals=0x0, locals=0x7ffff7f455b0, args=0x0, argcount=10898920, argcount@entry=0, kws=0xcf30, kws@entry=0x0, kwcount=0,
defs=0x0, defcount=0, kwdefs=0x0, closure=0x0) at Python/ceval.c:3585
#20 0x00007ffff7a5d8fb in PyEval_EvalCode (co=co@entry=0x7ffff7ed6150, globals=globals@entry=0x7ffff7f42508, locals=locals@entry=0x7ffff7f42508) at Python/ceval.c:773
#21 0x00007ffff7a79bb4 in run_mod (mod=mod@entry=0x6f74d8, filename=filename@entry=0x7ffff7e44ad0, globals=globals@entry=0x7ffff7f42508, locals=locals@entry=0x7ffff7f42508, flags=flags@entry=0x7fffffffe2c0,
arena=arena@entry=0x66e900) at Python/pythonrun.c:2180
#22 0x00007ffff7a7bde5 in PyRun_FileExFlags (fp=0x676c60, filename_str=<optimized out>, start=<optimized out>, globals=0x7ffff7f42508, locals=0x7ffff7f42508, closeit=1, flags=0x7fffffffe2c0)
at Python/pythonrun.c:2133
#23 0x00007ffff7a7cdb3 in PyRun_SimpleFileExFlags (fp=0x7ffffffe9660, filename=0x7ffff7f03d10 "triangle-ex1-naive-refine.py", closeit=1, flags=0xb0e4c0) at Python/pythonrun.c:1606
#24 0x00007ffff7a9301c in run_file (p_cf=<optimized out>, filename=<optimized out>, fp=<optimized out>) at Modules/main.c:319
#25 Py_Main (argc=-136033744, argv=0x7ffff7f03cf0) at Modules/main.c:751
#26 0x0000000000400af6 in main ()
I can reproduce the crash now. One issue with your code is that you double the point at (1,0) in your points list, when that's not necessary. (round_trip_connect closes the facet loop for you. But that turns out to not be essential for the crash.