Martin Liška | 3 Aug 2010 15:40
Picon

Swig - add function into module init

hi, I am using SWIG to wrap my C code. My question is if it is posible
to add function(wrapped) which will be called when module is imported
?

Thanks

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
Josh Cherry | 3 Aug 2010 16:26
Picon
Favicon

Re: Swig - add function into module init


On Tue, 3 Aug 2010, Martin Li~Zka wrote:

> hi, I am using SWIG to wrap my C code. My question is if it is posible
> to add function(wrapped) which will be called when module is imported
> ?

Section 5.6.4 of the manual describes %init, which likely can accomplish 
what you want.

Josh

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
Martin Liška | 3 Aug 2010 17:55
Picon

Re: Swig - add function into module init

That is exactly what I am looking for ;)

Thank you

2010/8/3 Josh Cherry <jcherry <at> ncbi.nlm.nih.gov>:
>
>
> On Tue, 3 Aug 2010, Martin Li~Zka wrote:
>
>> hi, I am using SWIG to wrap my C code. My question is if it is posible
>> to add function(wrapped) which will be called when module is imported
>> ?
>
> Section 5.6.4 of the manual describes %init, which likely can accomplish
> what you want.
>
> Josh
>
>

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
Ed Mazur | 3 Aug 2010 23:13
Picon
Gravatar

Memory leak in PHP director class

Hi,

I seem to be leaking memory when a director class passes a complex
type (i.e. std::vector<int>) to PHP. Here are my files:

http://pastebin.com/VqwJtJGh - directorin.i
http://pastebin.com/MAWDhk64 - FooClass.hpp
http://pastebin.com/4vRe047i - directorwrap.cpp
http://pastebin.com/mQsg9DaN - test.php

test.php triggers a director method call with a simple argument type
(int) 5,000 times, printing the memory usage after every 1,000
invocations. It then does the same with a complex type
(std::vector<int>). Here is its output:

$ php test.php
calling simple
686,360
686,568
686,568
686,568
686,568
calling complex
686,568
1,599,184
2,576,936
3,628,392
4,532,432

As you can see, memory only grows when passing back a complex type.
(Continue reading)

Ed Mazur | 4 Aug 2010 14:35
Picon
Gravatar

Re: Memory leak in PHP director class

I solved this. If I set $intVector to null inside the callback, this
takes care of 90% of the problem:

public function overrideMeComplex($intVector) {
    $intVector = null;
}

$ php test.php
calling simple
688,784
688,992
688,992
688,992
688,992
calling complex
688,992
761,456
833,456
905,456
977,456

If I also add an efree() between lines 871 and 872 of
directorin_wrap.cpp, the leak goes away completely:

efree(classname->value.str.val);

$ php test.php
calling simple
688,784
688,992
(Continue reading)

Ben Hindman | 5 Aug 2010 19:55
Favicon

Director upcalls and exceptions

I am trying to use SWIG to call from a C++ library into Java code (using Director's) and noticed that if a runtime exception is thrown in the Java code then it gets swallowed by a line generated by SWIG that looks something like:

  if (jenv->ExceptionOccurred()) return ;

or in other places:

  if (jenv->ExceptionOccurred()) return c_result;

Is this the expected behavior? Is there a mechanism for doing something if an exception has occurred rather than just swallowing it?

I appreciate any insight you have on this matter. Thanks so much for your time!

Benjamin.
------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Johan Hake | 6 Aug 2010 23:18
Picon
Gravatar

typdef of struct and %rename

Hello!

When a typedef is used for declaring a struct I cannot use %rename on its 
attributes. Does anyone have a explaination/fix/workaround for that. I am 
dealing with an external library so I cannot change the declarations...

See attached minimal example.

Johan

rename.i
-------------------------------------------
%module rename
%{
typedef struct {
  float x;
  float y;
}Vec1;

struct Vec2{
  float x;
  float y;
};

%}

%rename(z) Vec1::x; // Does not work
%rename(z) Vec2::x; // Does work

typedef struct {
  float x;
  float y;
}Vec1;

struct Vec2{
  float x;
  float y;
};

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
Romulo | 9 Aug 2010 02:43
Picon

Python and Swig + boost::shared_ptr

Hi there, good night everybody.


I started to use SWIG two days ago to expose my library interface to python. I built the interface file and everything is working well except for boost::shared_ptr objects.
Those objects are never returned from functions that return them and instead i get a SwigPyObject. I tried to debug the object, printing the class and the methods and got this:

Class: <class 'SwigPyObject'>
Functions: ['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__int__', '__le__',
'__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'diso
wn', 'next', 'own']

For other type of objects i always get the correct expected output:

Class: <class 'cgtech.sceneManager_t'>
Functions: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
 '__lt__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshoo
k__', '__swig_destroy__', '__weakref__', 'create2DText', 'createDynamicModel', 'createLight', 'draw', 'loadMap', 'this', 'thisown']

So now i'm wondering what i can possible be doing wrong to get this kind of behavior on shared_ptr. I'm including a snippet of the interface file and a snippet
of produced wrap cxx code. Thanks for your time, SWIG rules =]

====================
library.i
====================

%module windowLib
%{
  #include "lib.h"
  using namespace cg;
%}

%include <boost_shared_ptr.i>
%include <pyabc.i>
%include <std_list.i>
%include <std_map.i>
%include <std_string.i>
%include <std_vector.i>

// Children of abstract classes needs explicit declaration.
%feature("notabstract") cg::win32RenderWindow_t;

// shared_ptr needs explicit declaration. 
// This should come before any usage of shared_ptr on code.
%shared_ptr(cg::renderWindow_t)
%shared_ptr(cg::win32RenderWindow_t)

...

// Interfaces
%include "renderWindow.h"


====================
one of the library_wrap.cxx functions that return boost::shared_ptr
====================

SWIGINTERN PyObject *_wrap_engine_t_createWindow__SWIG_0(PyObject *SWIGUNUSEDPARM(self), int nobjs, PyObject **swig_obj) {
  PyObject *resultobj = 0;
  std::string *arg1 = 0 ;
  vector2_t *arg2 = 0 ;
  bool arg3 ;
  int res1 = SWIG_OLDOBJ ;
  void *argp2 = 0 ;
  int res2 = 0 ;
  bool val3 ;
  int ecode3 = 0 ;
  SwigValueWrapper< boost::shared_ptr< renderWindow_t > > result;
  
  if ((nobjs < 3) || (nobjs > 3)) SWIG_fail;
  {
    std::string *ptr = (std::string *)0;
    res1 = SWIG_AsPtr_std_string(swig_obj[0], &ptr);
    if (!SWIG_IsOK(res1)) {
      SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "engine_t_createWindow" "', argument " "1"" of type '" "std::string const &""'"); 
    }
    if (!ptr) {
      SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "engine_t_createWindow" "', argument " "1"" of type '" "std::string const &""'"); 
    }
    arg1 = ptr;
  }
  res2 = SWIG_ConvertPtr(swig_obj[1], &argp2, SWIGTYPE_p_Vector2T_float_t,  0  | 0);
  if (!SWIG_IsOK(res2)) {
    SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "engine_t_createWindow" "', argument " "2"" of type '" "vector2_t const &""'"); 
  }
  if (!argp2) {
    SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "engine_t_createWindow" "', argument " "2"" of type '" "vector2_t const &""'"); 
  }
  arg2 = reinterpret_cast< vector2_t * >(argp2);
  ecode3 = SWIG_AsVal_bool(swig_obj[2], &val3);
  if (!SWIG_IsOK(ecode3)) {
    SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "engine_t_createWindow" "', argument " "3"" of type '" "bool""'");
  } 
  arg3 = static_cast< bool >(val3);
  result = cg::engine_t::createWindow((std::string const &)*arg1,(Vector2< float > const &)*arg2,arg3);
  resultobj = SWIG_NewPointerObj((new boost::shared_ptr< renderWindow_t >(static_cast< const boost::shared_ptr< renderWindow_t >& >(result))), SWIGTYPE_p_boost__shared_ptrT_renderWindow_t_t, SWIG_POINTER_OWN |  0 );
  if (SWIG_IsNewObj(res1)) delete arg1;
  return resultobj;
fail:
  if (SWIG_IsNewObj(res1)) delete arg1;
  return NULL;
}




------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Arnleif Bjørstad | 9 Aug 2010 20:06
Picon
Favicon

Wrapping to C#, pointers, double pointers as argument output

Hi

 

Using MS Visual Studio 2008, I have set up a solution project like those in Swig examples for C#. My test application loads the dll, but so far I only can get the application to report its version number, that is returning an integer.

 

I understand that I need to do some extra work because SWIG cant know what is input, output, or both input/output.

 

One example is:

 

short LC_OpenSos(const char *fil,short sModus,short sNyIdx,short sVisStatus,LC_FILADM **ppFil, short *o_stat);

 

It is the last 2 arguments that gives me trouble.  They are both out-parameters.

 

LC_FILADM **ppFil (pointer to a struct)

short *o_stat

 

Reading about typemaps and looking into the typemaps.i file I have tried to correctly wrap the pointer “o_stat”.

In a preliminary test I did add a proxy function to the c++ program to reduse the pointer **ppFil to *pFil and the interface file looks like this:

 

/* File : Fyba.i */

%module FybaSharpTest

 

%{

#include "fyba.h"

#include "stdafx.h"

#include "Prox.h"

%}

                                    

%include "fyba.h"

%include "Prox.h"

 

// %ignore LC_OpenSos(const char *fil,short sModus,short sNyIdx,short sVisStatus,LC_FILADM **ppFil, short *o_stat);

 

%include "typemaps.i"

        %apply short *INOUT { short *o_stat };

short LC_OpenSos2(const char *fil,short sModus,short sNyIdx,short sVisStatus,LC_FILADM *ppFil, short *o_stat);

 

 

 

I would appreciate if anyone could helpe me with good examples, wrapping to C#.

I have searched the internet, but cant finde much useful examples.

 

 

Regards

 

Arnleif

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Ken Watford | 10 Aug 2010 21:30
Picon

Template arithmetic bug?

Using SWIG 2.0.0, I seem to be having some trouble wrapping templates
that include simple template arithmetic. As a simple example:

/* -------------------- blah.i ----------------- */
%module blah

%{
#define SWIG_FILE_WITH_INIT
%}

%inline %{

template<int N> class Foo {
public:
  int v() { return N; }
};

template<int N> class Bar {
public:
 Foo<N> x;
 Foo<N+1> y;
 Bar(Foo<N> xx, Foo<N+1> yy) : x(xx), y(yy) {}
};

%}

%template(Foo1) Foo<1>;
%template(Foo2) Foo<2>;
%template(Bar1) Bar<1>;
/* -------------- end blah.i ------------------ */

SWIG does not complain, and this code would otherwise compile.
However, the wrappers do not compile because of things like this:

SWIGINTERN PyObject *_wrap_new_Bar1(PyObject *SWIGUNUSEDPARM(self),
PyObject *args) {
  PyObject *resultobj = 0;
  Foo< 1 > arg1 ;
  SwigValueWrapper< Foo< N+1 > > arg2 ;
/* snip */

N+1 should have been evaluated by the template parser rather than
dumped whole here, where no N exists.

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 

Gmane