开发者

Python/SWIG: GC Object already tracked when trying to use a C function to dereference a pointer, from SWIG

开发者 https://www.devze.com 2023-03-22 04:51 出处:网络
I have an issue where I\'m dealing with WORDs (2 byte unsigned integers). Here are the commands I usually run

I have an issue where I'm dealing with WORDs (2 byte unsigned integers). Here are the commands I usually run

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. 

At this point, my program crashes. It throws an exception (reading violation) or some GC Object Already tracked error. The thing is - I have a string print function that never fails when I have it print. When I try to get the word to print, I do get errors.

This is my wordptr initiating function:

unsigned int * wordInit () {
    unsigned int * d = malloc ( sizeof ( unsigned int ) );
    *d = 0;
    return d;
}

This is my printing function:

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

I've no idea what I'm doing wrong here but these crashes are very erratic and annoying.

0

精彩评论

暂无评论...
验证码 换一张
取 消