Python/SWIG:GC对象在尝试使用C函数从SWIG取消引用指针时已经被跟踪

2024-04-20 04:09:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在处理单词(2字节无符号整数)时遇到了一个问题。以下是我通常运行的命令

import mySimLib
mySimLib.init()
strPtr = mySimLib.strInit( 200 )    #where 200 is the number of characters I want in the
                           #string. strInit returns a malloc'd pointer
wPtr = mySimLib.wordInit ()         # where wordInit returns a malloc'd pointer to a WORD.
mySimLib.Write ("Title", "Data", 4) # 4 is the number of bytes required to store data
mySimLib.Search ("Title", strPtr, 200, wPtr) #Search finds the record with same title,
      #copies the data into strPtr up to the number of bytes in the record - as long as
      #the number of bytes in the strPtr is greater. 

mySimLib.printWord (wPtr) #Since I cannot use python to dereference word pointers, I call a C function to print it out. 

此时,我的程序崩溃了。它抛出异常(读取冲突)或某些GC对象已跟踪错误。问题是-我有一个字符串打印函数,当我打印它时,它永远不会失败。当我试图把这个词打印出来时,我确实会出错。在

这是我的wordptr初始化函数:

^{pr2}$

这是我的打印功能:

void wordPrint (unsigned int * d){
    printf ("\nWptr: %d",*d);
}

我不知道我做错了什么,但这些碰撞是非常不稳定和恼人的。在


Tags: ofthetoinnumberbytesiswhere