MapInfo Pro Developers User Group

 View Only

System.AccessViolationException when running a .py script using "run application" through CommonUtils.do

  • 1.  System.AccessViolationException when running a .py script using "run application" through CommonUtils.do

    Posted 10-04-2023 07:34

    Hi,

    I have a python addin called ModuleLoader.py that dynamically creates tabs, groups and controls according to a .json file read in at runtime. The scripts for these controls are either .MBX scripts or .py scripts, which are held in a custom class and assigned to the control with a call to the AddinUtil.create_command() function on the class's call method:

    #.......
    #Check if existing command id was supplied
    if isinstance(control[fConComm], int):
         newControl.CommandId = control[fConComm]
    else:
         #Create new custom command object and append it to custCommands list for dereferencing later on
         #print("Attaching script {}".format(self.relativePath(control[fConComm]))
         self._custCommands.append(custCommand(self.relativePath(control[fConComm])))  
         newControl.Command = AddinUtil.create_command(self._custCommands[-1].call)
    #.......
    #Container object, holds path to script in memory and allows MapInfo to call later on when button is clicked
    class custCommand():
        def __init__(self, path):
            self.path = path
    
        def call(self, sender):
            CommonUtil.do('run application "{}"'.format(self.path))

    This works for .MBX files with no issue, however when I try to load .py files in after MapInfo startup, MapInfo crashes and the following error shows up in the error log:

    If I run the script from MapInfo's MapBasic console window or tool extensions window, it works fine and the control will work from then on. Looking at the Microsoft Documentation for System.AccessViolationException:

    An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access.

    I don't really understand the MapInfo codebase, so can anyone help me identify what the issue is and whether the fix is something I can do on my end or not?

    I've attached my code and the log as a text file.



    ------------------------------
    Romano Agostini
    Knowledge Community Shared Account
    ------------------------------

    Attachment(s)

    zip
    ModuleLoader.zip   34 KB 1 version
    txt
    Log.txt   6 KB 1 version