Jump to content
Sign in to follow this  
fjames

Prevent External DLL from Exiting Application

Recommended Posts

I was wondering if there is a way to prevent any external DLL calls from closing the Application.

I ran into an issue with matplotlib raising an error, and after the error, the Application closed, and I cannot find a way of 'catching' the exception to prevent the crash.

Stepping through the execution it seems that the call to PyRun_String in TPythonEngine.Run_CommandAsObjectWithDict never returns, and the application is terminated, and would like a way of preventing this.

 

I have been able to reproduce this problem in Demo01 by performing the following steps:

 

  1. Install Anaconda 32bit
  2. Create new environment (Python 3.6.12) and install matplotlib using 'conda install matplotlib' in the new environment
  3. Start Demo01 in the new environment
    1. Can either start Demo01.exe from activated cmd prompt, or just open the IDE from an activated cmd prompt that way you can debug in the environment as well
  4. Change script to simple: 'from matplotlib import pyplot as plt; plt.plot(range(10)); plt.show()'
    1. What results is a popup much like the one here.
    2. After clicking OK, the program exits
  5. Its worth noting, that I was able to overcome the above issue by following the steps in this answer, however I foresee more issues like this in the future, and I would like some way of preventing my application from crashing if one of Python's third party modules encounters an error. 

 

Thanks for any advice!

  

Share this post


Link to post

Any code can terminate the process. The way you avoid this is to ensure that the code you run doesn't lead to fatal errors. In this case it sounds like the issue is that you need to make sure that your environment has the correct packages installed. 

Share this post


Link to post

That is the answer I was afraid of. That means crashes like this are almost impossible to prevent. Making "sure that your environment has the correct packages installed" is very tricky it seems. For example in the matplotlib example, matplotlib seems to be installed correctly and only produces error calling 'show()', and actually if I start python directly from the activated cmd prompt, matplotlib executed successfully, so the problem was resulting from some dlls not being in the correct place when being run from within a Delphi Application (but installing qt and pyqt put the dlls in a place that works from both within and outside a Delphi application). So to properly ensure the environment was correct, I would need to know all the dlls needed by each python module, and ensure they are in the place that the module is expecting.
 

I guess if you are willing to allow the importing of third-party modules, this is a risk you have to take.

 

Thanks for the quick response!

Share this post


Link to post

You need to use RPyC to run your Python code - this decouples the Python from your main app. Check the source in PyScripter to see how it's done. There's code for internal interpreter and remote interpreter - you need the code in the latter.

Share this post


Link to post

Exception raised by a DLL function will crash the app. So you can ask DLL developers /change DLL yourself to handle all exceptions.

Share this post


Link to post
1 hour ago, Der schöne Günther said:

Running the code in an auxiliary process instead of your own could also be an option if you cannot fix the cause and only battle the symptoms.

That's what RPyC would gain you

  • Thanks 1

Share this post


Link to post

Sometimes it helps to trap the SystemExit  exception.   This would avoid many abnormal application exits. 

                try:
                   """ Add your Python code here"""
                except SystemExit as e:

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×