I don’t think the illegal memory access is related to the code usage in your script, but is an internal bug.
Could you post an executable code snippet and add the information about your setup, please?