# How to debug (more or less) random segfaults?

I ran into a wall with my add on development. (I am still a beginner with python and programming and add on development, so please be lenient).
I am running a modal operator to do some changes on the selected object (via raycast i get the face index under mouse cursor and on mouse click the mesh content gets deleted and new content, depending on the face index, will be generated).
Sometimes this works, sometimes it segfaults.
It segfaults more often when in texture display mode, less often in material display mode and even less often in solid display mode.
The crash happens after the new mesh content is created but before the modal operator checks for new events. (In the script itself nothing should happen inbetween).
So i used pdb.set_trace() to set a breakpoint right after the content creation and step through everything that happens after that. There is actually a lot of stuff going on (mostly (re)drawing the Ui afais ... it takes ages to step through everything).
Sadly it never crashes when stepping through it so i still do not know when (and why) exactly the crash happens and now i am lost, having no idea how to find the error.

Edit 1:

I striped down the code a bit. As it is still quite big and a certain material is needed, so, here a blend file:

http://www.codexofdreams.com/VA_Studio.blend

To produce the Error open the file (tested with Blender 2.79 and 2.79a) run the script, find the VA tab in the tool shelf, click the button in it and then click on the object. At least for me it sometimes works (The Part of the mesh under the cursor vanishes) but mostly it segfaults.

Edit 2: I startet Blender with --debug until now but stumbled across --debug-all. It gives a bit more information on what is going on and i might have found the culprit: Before the EXCEPTION_ACCESS_VIOLATION there was no clue with only --debug, but now with --debug-all right before the crash it says:

WM_operator_last_properties_init: loading previous properties for 'VIEW3D_OT_select'

I am not 100% sure what it means but since i use left mouseclick in the modal operator as event to call the function that changes the mesh (delete all geometry and puts new in) and also to select objects in the scene there seems to be clash of those two maybe. At least when i use a keyboard key to call the function it does not crash (hopefully it remains like this).
Now i have to look if i can find a way to overwrite the mouse click selection while modal is running (prefered... if i cannot find it out on my own i might do a new question) or if i need to use keyboard instead. So this is now at least half solved.

• Extremely difficult to debug code sans code – batFINGER Mar 1 '18 at 17:08
• WIll post the code this weekend, no time to strip it down to the problem before. – CoD Mar 2 '18 at 10:41
• Recommend you only raycast on a click event. Had a quick look and can confirm it crashes for me on linux. After the first or a number of "Negative naterial index!" warnings in the console. I believe it has something to do with your generated mesh data. – batFINGER Mar 2 '18 at 19:07
• In the not stripped down version the raycast is also running for the bgl highlighting (it works fine). The generated geometry should also be fine. In the not sriped down versions i do different operations with it without running into problems (using the same code building it): youtu.be/yOqmrxiJpDQ?t=53 ). I will to call the raycast function on click and use this results for the geometry change and using the modal running one only for bgl highlighting... will report back if it makes a difference. – CoD Mar 2 '18 at 19:15
• You can build a debug build Blender yourself, and enable ASAN to get much better insight into what is going wrong, what memory is read/written, and by which code. – dr. Sybren Mar 3 '18 at 18:39