c++ - Python C-extension class with a dynamic size -
c++ - Python C-extension class with a dynamic size -
i seek write c-extension contains python class.
this class takes filename parameter fname
of constructor, loads file memory , stores config.
below have:
typedef struct { pyobject_head xmlconfig m_cfg; } pyagent; static int pyagent_init(pyagent *self, pyobject *args) { const char *fname; if (!pyarg_parsetuple(args, "s", &fname)) { homecoming null; } self->m_cfg.load(fname); homecoming 0; } static pytypeobject pyagenttype = { pyobject_head_init(null) 0, /* ob_size */ "classify.agent", /* tp_name */ ... } ...
i segmentation fault when seek load file. understand happens because pyagent struct
has object size increases because of memory allocation file.
i've tried utilize pyobject_var_head
macro doesn't help.
can give clue or working similar example?
maybe self->m_cfg
needs initialized first? calling c++ method on it, didn't phone call constructor first. struct allocated c code, not c++, doesn't know needs build field.
there ways manually phone call constructor (something new(&self->m_cfg) xmlconfig()
if remember correctly), easiest have instead field xmlconfig *cfg
, , allocate , free needed usual syntax of c++ operators new
, delete
(the latter set in tp_dealloc
).
python c++ c segmentation-fault python-c-extension
Comments
Post a Comment