ctypes不允许多次取消引用指针

时间:2017-08-25 03:07:48

标签: python c++ pointers ctypes

我一直在努力通过ctypes将一些C ++类扩展到python。 我首先尝试通过GDB和代码验证地址不等于NULL。它确实不等于NULL。我尝试通过printf打印指针并打印出地址,但涉及指针的下一行代码会引发分段错误。奇怪的是,没有printf语句,printf语句之后的下一行代码就可以工作。但是,涉及指针的下一个函数会抛出一个分段错误,就像printf一样。

以下是我的参考代码:

C ++ - > C ctypes wrapper

#include <jetfuelmedia.h>

#define MAX_FILE_NAME_SIZE 1024

extern "C"{
jetfuel::media::Music *Music_new(){
    return new jetfuel::media::Music();
}

void Music_delete(jetfuel::media::Music *music){
    puts("Destroyed Music object");
    delete music;
}

bool Music_is_music_playing(){
    return Mix_PlayingMusic();
}

bool Music_is_music_paused(){
    return Mix_PausedMusic();
}

bool Music_load_music_file(jetfuel::media::Music *music,
                           const wchar_t *musicfilepath){
    char musicfilepathchar[MAX_FILE_NAME_SIZE];

    wcstombs(musicfilepathchar,musicfilepath,
            MAX_FILE_NAME_SIZE);

    puts("Music file is:");
    puts(musicfilepathchar);
    puts("Music ref is:");
    printf("%p",music);

    if(!music->Load_music_file(musicfilepathchar)){
        puts(musicfilepathchar);
        puts(Mix_GetError());
        return false;
    }

    puts("C/C++ code ran");

    return true;
}

bool Music_play(jetfuel::media::Music *music){
    puts("C/C++ Play code ran");
    return music->Play();
}

void Music_pause(jetfuel::media::Music *music){
    music->Pause();
}

void Music_resume(jetfuel::media::Music *music){
    music->Resume();
}

const char *Get_music_error(){
    const char *sdlerror = Mix_GetError();

    if(sdlerror == NULL){
        return "Music object was equal to NULL";
    }
    return Mix_GetError();
}
}

Python类包装器:

from ctypes import cdll
from ctypes import c_wchar_p
from ctypes import c_void_p
from ctypes import c_bool

class music():  
   _jetfuel = None;
   _musicref = None;

    def __init__(self,jetfuelsofilepath):
        self._jetfuel = cdll.LoadLibrary(jetfuelsofilepath);
        self._musicref = self._jetfuel.Music_new();

    def __enter__(self):
        return self;

    def __exit__(self, exc_type, exc_value, traceback):
        if(self._musicref != None):
            self._jetfuel.Music_delete(c_void_p(self._musicref));

    def is_music_playing(self):
        return self._jetfuel.Music_is_music_playing();

    def is_music_paused(self):
        return self._jetfuel.Music_is_music_paused();

    def load_music_file(self, musicfilepath):
        loadmusicfile = self._jetfuel.Music_load_music_file;
        loadmusicfile.argtypes = [c_void_p, c_wchar_p];
        loadmusicfile.restype = c_bool;

        return loadmusicfile(c_void_p(self._musicref), 
                         c_wchar_p(musicfilepath));

    def play(self):
        return self._jetfuel.Music_play(c_void_p(self._musicref));

    def pause(self):
        self._jetfuel.Music_pause(c_void_p(self._musicref));

    def resume(self):
        self._jetfuel.Music_resume(c_void_p(self._musicref));

    def print_debug(self):
        print("Music ref is ="+str(c_void_p(self._musicref)));

    def get_music_error(self):
        return self._jetfuel.Get_music_error();

如何使用可以多次解除引用的指针使这段代码工作?

如果这涉及任何问题,我的系统信息是:

  • Ubuntu 17.04 64位
  • GCC(G ++)5.4.1
  • GDB 7.12.50
  • Python 3.6
  • SDL 2.0.3

1 个答案:

答案 0 :(得分:0)

您忘了设置restype的{​​{1}}:

Music_new

默认值为def __init__(self,jetfuelsofilepath): self._jetfuel = cdll.LoadLibrary(jetfuelsofilepath) self._jetfuel.Music_new.restype = c_void_p self._musicref = self._jetfuel.Music_new() ,这会弄乱指针。其他功能需要类似的处理。为了保持整洁,我建议你在一个地方完成所有这些工作(例如函数int)。