程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[c/c++ python] program crashes miraculously. Please explain what metaphysical programming is

編輯:Python
The phenomenon and background of the problem

Write another thing mod function , Then use the python Of c/c++SDK, Then I started to use it very well , result mingw The truth of metaphysics scared me , The initial source code of that part is as follows :( With IDE:Codeblocks 2020 mingw 64 SEH)

 al("Getting initialized values...");// Log output function ///Getting inited values //outs: #define outs(A) cout << string(#A ":\n") << A << endl; outs(PyObject_GetAttrString(GetPm().mlcPointer,MENU_STRINGS_ITEM)); showingStrings = getStringLists(PyObject_GetAttrString(GetPm().mlcPointer,MENU_STRINGS_ITEM)); outs(showingStrings[0]);
Operation results and error reporting contents

This is the output :

[2022-02-09 18:51:56]:652ms:This mod "More Titles" path "C:\Users\yd\AppData\Roaming\XXXX\XXXX\Mods\MT.mtmod"'s OnInit function returns False in python.It loaded fail!load mod "C:\Users\yd\AppData\Roaming\StudyAll\MineTerraria\Mods\MT.mtmod" fail![2022-02-09 18:51:56]:662ms:Getting initialized values...GetPm().mlcPointer:0xbcd7268Process returned -1073741819 (0xC0000005) execution time : 8.593 sPress any key to continue.
My solution ideas and tried methods

Then I added two useless words ( The purpose is to see python Did you move the pointer , Cause an error ), Modify the code as follows :

 al("Getting initialized values..."); ///Getting inited values outs(PyObject_GetAttrString(GetPm().mlcPointer,MENU_STRINGS_ITEM)); outs(PyObject_GetAttrString(GetPm().kernel,"mlc"));// The first sentence  outs(GetPm().mlcPointer);// The second sentence  showingStrings = getStringLists(PyObject_GetAttrString(GetPm().mlcPointer,MENU_STRINGS_ITEM)); outs(showingStrings[0]);

As a result, metaphysics emerged :

PyObject_GetAttrString(GetPm().mlcPointer,MENU_STRINGS_ITEM):0PyObject_GetAttrString(GetPm().kernel,"mlc"):0xbca7268GetPm().mlcPointer:0xbca7268showingStrings[0]:Try playing Minecraft!!!![2022-02-09 18:55:49]:636ms:Checking mods' logs...*****************************************************[2022-02-09 18:55:49]:646ms:Here are the mod logs that are listed:''*****************************************************

It didn't break down !

What I want to achieve

I just want to know why ( I also tried Py_Incref(GetPm().mlcPointer) And Py_Decref(GetPm().mlcPointer) Let's see if it's a reference problem , However , It is a pity that , collapsed , It should not be a memory reference problem )
###### The external cause of the collapse
The following are loaded " module "python Code :

# This is api, Not in high school yet , English is not good , Many English sentences are grammatically incorrect , Don't spray # mtmod_loader.py'''Import Suggestions: from mtmod_loader import *Using Docs:Loading dods has many stepsWhile in version 1.0,it has only 1 step: Initializing Initializing: To init,you need to have a function OnInit()'''# We suggest you not to change mlc directlyclass ModLoaderConfig: #Strings that shows on the menu menuStrings = [] # Very Warning:Do not change this value,or mod loading proc will fail! # now loading proc!DO NOT CHANGE IT!!THIS IS VERY IMPORTANT DATA nowProc = 0 # the mod's log modLog = '' #the statuses menuLock = False'''The Manifest file's attributesYou can easily get them'''class ManifestFileSets: # Mod Name ModName = "" # This Mod's Version ModVersion = "" # The Mod's Creator ModAuthor = "" # The Mod's Description ModDescription = "" # The Only Package ID,Make sure this id is the only one!!!! ModPackage = "" mlc = ModLoaderConfig()mf = ManifestFileSets()apiVersion = "api-1.0"MenuLocked = 2NowIsNotIniting = 1Suc = 0Fail = -1'''Lock the menu,other mods cannot change menuString if they follows the usual steps'''def lockMenu(): mlc.menuLock = True'''Set the strings shows upon the menu'''def setMenuString(menuStr): if(mlc.menuLock): return MenuLocked if(mlc.nowProc == 0): mlc.menuStrings = menuStr return Suc else: return NowIsNotInitingdef sms(menuStr): return setMenuString(menuStr)'''Append the strings that shows on the menu'''def appendMenuString(nstr): mlc.menuStrings.append(str(nstr))def ams(nstr): appendMenuString(nstr)'''Get the strings that shows on the menu'''def getMenuString(): return mlc.menuStringsdef gms(): return mlc.menuStrings'''Get the loading proc now'''def getLoadingProcess(): return mlc.nowProcdef glp(): return mlc.nowProc'''Write the program log'''def writeModLog(strlg): mlc.modLog += str(strlg)def wml(strlg): writeModLog(strlg)

This is the code of the module

# main.pyfrom mtmod_loader import *def OnInit(): print('Initializing mod',mf.ModName); l = getMenuString() l.append("More Titles is a mod which adds a lots titles to the main menu screen!") l.crashDump() #list There is no such member in , This is for testing crashes  setMenuString(l) print(getMenuString()) print('Done initializing mod',mf.ModName); return False



Take the answer :

You have to set your own breakpoints for this kind of problem , Then step through the debugging , To find the problem . Only from the information provided above , It is difficult for others to help you find out the root cause of the problem .
in addition , You can try boost.python Library to export C/C++ Write modules to python,boost.python Interface is better than Python Self contained SDK Easy to use , At least closer to C++, More humanized .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved