hoffstadt / hoffstadt/DearPyGui

Random segmentation faults when adding and deleting textures

Open
#2,159 0 comments 0 reactions 0 assignees View on GitHub
state: pending type: bug
Dominant language
C++
Stars
15.6k
Forks
783
PR merge metrics
No merged PRs in 30d

Description

## Version of Dear PyGui

Version: 1.9.1
Operating System: macOS 13.3, M2 chip

## My Issue/Question

I was having random crashes related to adding and deleting a static texture (as revealed by `python -X faulthandler texturebug.py`). As a workaround I found that putting a `time.sleep(0.001)` prevented the crashes in some cases. In other cases I needed a delay of up to 1 second to reliably prevent a seg fault. This to me suggests some kind of race condition. Swapping between `add_raw_texture`, `add_static_texture` and `add_dynamic_texture` made no difference.

This issue is related to #554, and I decided to open a new issue as I have additional information and the old issue was initially opened in v0.6.157 and potentially stale... Feel free to close in favour of that one if you feel that's best and apologies for the noise.

I notice the bug only seems to occur if the texture it being shown by a component. So perhaps the cause of this is the frame attempting to render the texture while it is being deleted?

## To Reproduce

Steps to reproduce the behavior:
1. Save the code below as `texturebug.py`
2. Run with `python -X faulthandler texturebug.py` to see the seg fault stack trace. You can also just run `python texturebug.py`
3. There are multiple random ways to see the bug, which sometimes doesn't occur. Clicking add and then delete occasionally triggers a segfault. I noticed this happens more often after a few runs. You can also click "Do it Forever" which will add and delete the texture in a while loop while counting iterations. For smaller textures I noticed the crash happens quicker (but in my app bigger textures caused it). The crash does not occur if the texture registry showing the texture is hidden.
4. Example Error:
![image](https://github.com/hoffstadt/DearPyGui/assets/1703141/700bc24d-a2e6-4f7f-a2f1-11364029bcca)

## Expected behavior

Adding textures and deleting textures that have been added should not result in a segmentation fault

## Screenshots/Video

![image](https://github.com/hoffstadt/DearPyGui/assets/1703141/e3b02668-fee2-4aac-b5c2-78ec2b939ef1)

## Standalone, minimal, complete and verifiable example

```python
import dearpygui.dearpygui as dpg
import numpy as np

dpg.create_context()
dpg.create_viewport(width=800, height=600)
dpg.setup_dearpygui()

dpg.add_texture_registry(label="Demo Texture Container", tag="texture_registry", show=True)

width, height = 25, 25 #100, 100
texture = np.random.random(width*height*4) # random rgba data for an image width x height

def add_texture(sender, data):
dpg.add_static_texture(width=width, height=height, default_value=texture,
tag="texture_tag", parent='texture_registry')

def delete_texture(sender, data):
dpg.delete_item('texture_tag')

iterations = 0
def crash(sender, data):
global iterations
while True:
add_texture(None, None)
delete_texture(None, None)
iterations += 1
dpg.set_value('iterationcount', iterations)

with dpg.window(label="Texture Bug", width=200):
dpg.add_button(label="Add Texture", callback=add_texture)
dpg.add_button(label="Delete Texture", callback=delete_texture)
dpg.add_button(label="Do it forever", callback=crash)
with dpg.group(horizontal=True):
dpg.add_text("Iterations: ")
dpg.add_text(tag="iterationcount", default_value="0")

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
```
## Example 2:

(originally copied from tobyclh's comment in #554)
To reproduce click "add" then "delete". Crash happens on my machine within 2 tries. Note that if the window with the image is hidden, the crash doesn't happen.

```python
import dearpygui.dearpygui as dpg
dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

def add_widgets(sender, data):

with dpg.window(label="Secondary Window", tag='window', pos=[100,0]): # simple
#add_button("New Button")
width, height, channels, data = dpg.load_image("img.png")
with dpg.texture_registry(show=False):
dpg.add_dynamic_texture(width=width, height=height, default_value=data, tag="texture_tag")
dpg.add_image_button('texture_tag', label="image", show=True)
def delete_children(sender, data):
dpg.delete_item("window")
dpg.delete_item('texture_tag')

dpg.show_debug()

with dpg.window(label="Tutorial"):
dpg.add_button(label="Add Window and Items", callback=add_widgets)
dpg.add_button(label="Delete Window's Children", callback=delete_children)
dpg.show_viewport()

dpg.start_dearpygui()
dpg.destroy_context()
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.