0

I am currently switching my graphics toolkit over to use VAOs and VBOs instead of the legacy functions. I need a way to ensure that after the user's program runs, and even if it crashes, it will still clean up before execution stops. I also need the traceback to be printed, else the end user will not be able to see what went wrong with their code - or mine.

Here is a general overview of how my toolkit works: The end developer creates a PyQt5 app with a QOpenGLWidget in it. They import the shader and whatever shape classes they need from the shaderProgram file, then instantiate the shader. If they want to add a shape to the scene, they just instantiate a shape object, then call the shader's addShape function with the new shape. This is designed to be as easy as possible to use, but should offer lots of features when it's done.

test.py:

from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget

from shaderProgram import shader, cube
import sys

def run():
    app = QApplication([])
    window = mainWindow()
    window.show()
    sys.exit(app.exec_())

class mainWindow(QMainWindow):
    def __init__(self):
        super(mainWindow, self).__init__()
        self.setGeometry(0, 0, 500, 500)
        self.openGLWidget = QOpenGLWidget(self)
        self.shader = shader(self, self.openGLWidget)
        self.shader.resize(500, 500)
        self.openGLWidget.setGeometry(0, 0, 500, 500)
        self.cube1 = cube((0, 0, 0))
        self.cube2 = cube((1, 0, 0))
        self.cube3 = cube((0, 0, 1))
        self.shader.addShapes(self.cube1, self.cube2, self.cube3)
        self.shader.navigate(vVal = -45)
        self.shader.update()

if __name__ == '__main__':
    run()

block diagram of shaderProgram.py:

import stuff

class shader():
    def __init__():
    def addShapes():
    def update():
    def resize():
    def navigate():
    def initGL():
    def paintGL():

class shape():
class shape():
#...

GitHub repo: https://github.com/AwesomeCronk/pyGPU - note: I have not yet begun implementing the buffers because I do not want to test it without first making a safety/cleanup system, but this GitHub repo has the entire source as of now and shows the structure of the workflow.

Without modifying the way the end developer writes their code, how can I make sure the VAOs, VBOs, and custom shaders are cleaned up and deleted if there is a crash?

AwesomeCronk
  • 421
  • 6
  • 16
  • This is not the way to write software. Your effort should be to write correct code. Spending time on this could be better spent writing correct code that doesn't crash. – RamblinRose Apr 07 '20 at 02:21
  • Generally, when you have code that may throw an exception, you want to put that code within a `try/except` block. If you have code that you want to ensure runs after the block completes whether an exception is thrown or not, include a `finally` block at the end. – Todd Apr 07 '20 at 02:28
  • @RamblinRose I may not have made clear what I want. I want my code to act as a buffer for someone else’s code. If they run my code and their side crashes, I need to make sure that my code cleans up after itself. – AwesomeCronk Apr 07 '20 at 11:09
  • @Todd I looked at the try/finally``` option, but I don’t know that is would work. I believe the gl cleanup stuff can only be called from the paintGL and initGL functions. If they can be called from elsewhere, then I can have the end developer run their code in a try/finally or write a context manager for them to use. – AwesomeCronk Apr 07 '20 at 11:13
  • @AwesomeCronk maybe you want to include a `__del__()` method to your class to run code when an instance is gc'd?? – Todd Apr 07 '20 at 20:21
  • @AwesomeCronk... `sys.excepthook`?? an idea could be to register a global handler for exceptions.. and in your handler you register check the type of exception and other details to determine whether your class needs to clean up: https://stackoverflow.com/questions/6598053/python-global-exception-handling – Todd Apr 07 '20 at 20:28
  • @Todd Thanks for that. I'll be digging deeper there. – AwesomeCronk Apr 08 '20 at 02:50
  • @AwesomeCronk I hope it's workable for you. I thought of another idea that might apply - the `signal` module lets you register handlers for various events like `SIGSEGV`...`SIGTERM`... `SIGKILL`.. etc. I've used it myself before for capturing error information on crashes. – Todd Apr 08 '20 at 14:01

0 Answers0