[C++-sig] [Swig-user] A callback function with arguments causes error

Monty Taylor monty at inaugust.com
Mon May 14 20:27:07 CEST 2007


Hi Bruce,

I'm using a callback with arguments from Python using directors.

I've got this:

class BaseCallback {
  public:
  virtual ~BaseCallback() {};
  virtual void callback(int res, NdbTransaction * trans);

};


Then from python:

class PythonCallback(ndbapi.BaseCallback):

  def __init__(self, recAttr):
    self.recAttr=recAttr

  def __call__(self, *args, **kw):
    self.callback(*args,**kw)

  def callback(self, ret, myTrans):
    x=self.recAttr.get_value()

Works like a charm. Are you sure everything is working right with memory
ownership and that int *? I'd try it with just a normal int first to see
if that's the problem, then go in and figure out how to get Python to
allocate and pass the int* in such a way that it still exists when you
access it in the callback... but I could be way off base there.

Monty

Bruce Who wrote:
> Hi, all
> 
> Has nobody tried a callback function with arguments? I just want to
> implement an event-handling by doing this, but I donnot know how to
> make it work.
> 
> On 5/10/07, Bruce Who <bruce.who.hk at gmail.com> wrote:
>> Hi, all
>>
>> I have some callback functions in my .cpp files and I want to wrap
>> them into python scripts with swig. So I have tried this example
>> first: swigwin-1.3.31/Examples/python/callback. This example compiles
>> and runs well. But this callback function takes no arguments, so I
>> changed it a little:
>>
>> // example.h
>> class Callback {
>> public:
>>         virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
>>         // XXX this is the original statement
>>         // virtual void run() { std::cout << "Callback::run()" << std::endl; }
>>         virtual void run(int* pn) { std::cout << "Callback::run()" << (*pn)
>> << std::endl; }
>> };
>>
>>
>> class Caller {
>> private:
>>         Callback *_callback;
>> public:
>>         Caller(): _callback(0) {}
>>         ~Caller() { delCallback(); }
>>         void delCallback() { delete _callback; _callback = 0; }
>>         void setCallback(Callback *cb) { delCallback(); _callback = cb; }
>>         // XXX void call() { if (_callback) _callback->run(); }
>>         void call()
>>         {
>>                 if (_callback)
>>                 {
>>                     int i = 90;
>>                     // XXX _callback->run(i);
>>                     _callback->run(&i);
>>                 }
>>         }
>> };
>>
>>
>> As you see, I just add a int* argument to this callback. It compiles
>> but it cause python.exe to crash while running. Could anybody tell me
>> how to solve this? Any help would be appreciated!
> 
> 
> Bruce
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Swig-user mailing list
> Swig-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/swig-user




More information about the Cplusplus-sig mailing list