#include <iostream>
class Callback {
public:
virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
virtual void run(const std::string &s) { std::cout << s << 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; }
void call(const std::string &s) { if (_callback) _callback->run(s); }
};
In example.i
/* File : example.i */
%module(directors="1") example
%{
#include "example.h"
%}
%include "std_string.i"
/* turn on director wrapping Callback */
%feature("director") Callback;
%include "example.h"
In runme.php
# This file illustrates the cross language polymorphism using directors.
require("example.php");
# Class, which overwrites Callback::run().
class PhpCallback extends Callback {
function run($msg) {
//print "PhpCallback.run()\n";
print "PhpCallback ".$msg.PHP_EOL;
}
};
# Create an Caller instance
$caller = new Caller();
print "\n";
print "Adding and calling a PHP callback\n";
print "------------------------------------\n";
# Add a PHP callback.
$callback = new PhpCallback();
$callback->thisown = 0;
$caller->setCallback($callback);
$caller->call("hello");
$caller->delCallback();
# All done.
print "php exit\n";
Output
$php runme.php
Adding and calling a PHP callback
------------------------------------
PhpCallback hello
Callback::~Callback()
php exit
Valgrind output
$valgrind --leak-check=full --track-origins=yes php runme.php
==6660== Memcheck, a memory error detector
==6660== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6660== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6660== Command: php runme.php
==6660==
Adding and calling a PHP callback
------------------------------------
PhpCallback hello
==6660== Conditional jump or move depends on uninitialised value(s)
==6660== at 0x5F93AF: _zval_ptr_dtor (in /usr/bin/php)
==6660== by 0x631D4D: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x5FB09B: zend_call_function (in /usr/bin/php)
==6660== by 0x5FB40F: call_user_function_ex (in /usr/bin/php)
==6660== by 0x5FB461: call_user_function (in /usr/bin/php)
==6660== by 0xA7D0D25: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1353)
==6660== by 0xA7D20D7: Caller::call(std::string const&) (in /home/xvf/downloads/swig-2.0.4/Examples/php/callback/example.so)
==6660== by 0xA7D17E4: _wrap_Caller_call (example_wrap.cxx:1563)
==6660== by 0x68CE4C: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x6090A8: zend_execute_scripts (in /usr/bin/php)
==6660== Uninitialised value was created by a stack allocation
==6660== at 0xA7D0BF2: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1338)
==6660==
==6660== Conditional jump or move depends on uninitialised value(s)
==6660== at 0x5F93F5: _zval_ptr_dtor (in /usr/bin/php)
==6660== by 0x631D4D: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x5FB09B: zend_call_function (in /usr/bin/php)
==6660== by 0x5FB40F: call_user_function_ex (in /usr/bin/php)
==6660== by 0x5FB461: call_user_function (in /usr/bin/php)
==6660== by 0xA7D0D25: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1353)
==6660== by 0xA7D20D7: Caller::call(std::string const&) (in /home/xvf/downloads/swig-2.0.4/Examples/php/callback/example.so)
==6660== by 0xA7D17E4: _wrap_Caller_call (example_wrap.cxx:1563)
==6660== by 0x68CE4C: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x6090A8: zend_execute_scripts (in /usr/bin/php)
==6660== Uninitialised value was created by a stack allocation
==6660== at 0xA7D0BF2: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1338)
==6660==
==6660== Conditional jump or move depends on uninitialised value(s)
==6660== at 0x5F93AF: _zval_ptr_dtor (in /usr/bin/php)
==6660== by 0x5FAF2D: zend_call_function (in /usr/bin/php)
==6660== by 0x5FB40F: call_user_function_ex (in /usr/bin/php)
==6660== by 0x5FB461: call_user_function (in /usr/bin/php)
==6660== by 0xA7D0D25: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1353)
==6660== by 0xA7D20D7: Caller::call(std::string const&) (in /home/xvf/downloads/swig-2.0.4/Examples/php/callback/example.so)
==6660== by 0xA7D17E4: _wrap_Caller_call (example_wrap.cxx:1563)
==6660== by 0x68CE4C: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x6090A8: zend_execute_scripts (in /usr/bin/php)
==6660== by 0x5B5F52: php_execute_script (in /usr/bin/php)
==6660== by 0x42644D: ??? (in /usr/bin/php)
==6660== Uninitialised value was created by a stack allocation
==6660== at 0xA7D0BF2: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1338)
==6660==
==6660== Conditional jump or move depends on uninitialised value(s)
==6660== at 0x5F93F5: _zval_ptr_dtor (in /usr/bin/php)
==6660== by 0x5FAF2D: zend_call_function (in /usr/bin/php)
==6660== by 0x5FB40F: call_user_function_ex (in /usr/bin/php)
==6660== by 0x5FB461: call_user_function (in /usr/bin/php)
==6660== by 0xA7D0D25: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1353)
==6660== by 0xA7D20D7: Caller::call(std::string const&) (in /home/xvf/downloads/swig-2.0.4/Examples/php/callback/example.so)
==6660== by 0xA7D17E4: _wrap_Caller_call (example_wrap.cxx:1563)
==6660== by 0x68CE4C: ??? (in /usr/bin/php)
==6660== by 0x62D59A: execute (in /usr/bin/php)
==6660== by 0x6090A8: zend_execute_scripts (in /usr/bin/php)
==6660== by 0x5B5F52: php_execute_script (in /usr/bin/php)
==6660== by 0x42644D: ??? (in /usr/bin/php)
==6660== Uninitialised value was created by a stack allocation
==6660== at 0xA7D0BF2: SwigDirector_Callback::run(std::string const&) (example_wrap.cxx:1338)
==6660==
Callback::~Callback()
php exit
==6660==
==6660== HEAP SUMMARY:
==6660== in use at exit: 15,624 bytes in 27 blocks
==6660== total heap usage: 21,103 allocs, 21,076 frees, 3,672,898 bytes allocated
==6660==
==6660== 88 (32 direct, 56 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 12
==6660== at 0x4C2993D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6660== by 0x783B2C3: ???
==6660== by 0x78A804E: ???
==6660== by 0x75BDA99: ???
==6660== by 0x75BF988: ???
==6660== by 0x75C51B4: ???
==6660== by 0x6F17E72: ???
==6660== by 0x6F1F868: ???
==6660== by 0x6CE8127: ???
==6660== by 0x6096AE: zend_startup_module_ex (in /usr/bin/php)
==6660== by 0x615303: zend_hash_apply (in /usr/bin/php)
==6660== by 0x60CF29: zend_startup_modules (in /usr/bin/php)
==6660==
==6660== LEAK SUMMARY:
==6660== definitely lost: 32 bytes in 1 blocks
==6660== indirectly lost: 56 bytes in 2 blocks
==6660== possibly lost: 0 bytes in 0 blocks
==6660== still reachable: 15,536 bytes in 24 blocks
==6660== suppressed: 0 bytes in 0 blocks
==6660== Reachable blocks (those to which a pointer was found) are not shown.
==6660== To see them, rerun with: --leak-check=full --show-reachable=yes
==6660==
==6660== For counts of detected and suppressed errors, rerun with: -v
==6660== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 2 from 2)
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user