Eric Yen | 27 Jan 23:53 2015

SWIG_ConvertPtr failing - how do I diagnose the problem?

Hi Everyone,

I wrapped my C++ code using swig for python.
Normally, the code with a COM, and I can pass COM objects to functions.

Now, what I've done is wrapped my C++ code with SWIG, and used the comtypes package in Python to create my COM objects.

When I try to pass my Python-COM objects to SWIG I get errors,specifically, at lines that are trying to convert my Python COM objects, (or any Python Objects). 

e.g. 
SWIG_ConvertPtr(argv[1], 0, SWIGTYPE_p_ChannelPtr, 0)

This returns -1

My question is what should I do to fix the problem?

One of the potential causes, is that my SWIG code has no idea as to what a ChannelPtr is.
And it doesn't include the actual code for the COM.


  Eric
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Darryl L. Pierce | 27 Jan 17:23 2015
Picon

Ruby, C and rb_gc_mark for C structs

I have a C library that I'm wrapping for use by Ruby with Swig. In the
C library we have a type that can be used to hold onto a reference to
arbitrary types, including Ruby objects.

So my first step is to do up a simple proof-of-concept for our project,
which is posted here [1]. In my POC I have a type named pn_rubyref_t
that has a single field, a void * called ruby_object. And I can
successfully create a pure Ruby object and add it to the struct just
fine.

But what goes wrong is when garbage collection runs. Then my Ruby object
gets reaped and the POC app segfaults.

Reading the Swig documentation I see that there's the capability of
setting a mark function for C structs so that, when GC runs, we can mark
all child objects and I would be able to avoid this problem I'm facing.

However, my mark function is not getting called.

So what I've done is this:

%trackobjects;
%markfunc pn_rubyref_t "pn_rubyref_mark";
%include "proton/rubyref.h"

%header %{

  static void pn_rubyref_mark(void* ptr) { ... }
%}

I've also added some debugging output to my mark function above. But I
*never* see that output when GC is run, and I see my Ruby objects
getting reaped.

Can someone point me to a good example of setting a mark function in
Swig for a C library?

[1] https://github.com/mcpierce/Proton/tree/c-to-ruby-reference-gc-check
--

-- 
Darryl L. Pierce <mcpierce <at> gmail.com>
http://mcpierce.blogspot.com/
Famous last words:
   "I wonder what happens if we do it this way?"
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Daniel.Vollmer | 21 Jan 12:56 2015
Picon

Ignoring all methods that take a specific type as argument?

Hi,

while wrapping some C++ classes to Python, I would like to ignore a certain class of methods,
preferably without having to list them explicitly. All the methods take a specific type (a typedef
mapping to int) as the first argument.

It feels like SWIG is very close to being able to get this done using the %rename-directive with
regex-expressions to $ignore, but I could not get it work.

A bit more concretely (typed in, not real code):

typedef int ThreadIndex;

class Foo
{
public:
  void DoStuff(const ThreadIndex i); // should be ignored
  void DoOtherStuff(const int i); // should be wrapped
};

Has anyone had any success doing something like that? Any regex I tried in the %rename
would never match ThreadIndex, even giving fullname=1

I've tried something similar to the approach in
http://comments.gmane.org/gmane.comp.programming.swig.devel/20313 , i.e.
%rename("$ignore", fullname=1, regextarget=1) "Foo::.*(.* ThreadIndex .*";

Best regards

Daniel Vollmer

--------------------------
Deutsches Zentrum für Luft- und Raumfahrt e.V. (DLR)
German Aerospace Center
Institute of Aerodynamics and Flow Technology | Lilienthalplatz 7 | 38108 Braunschweig | Germany
------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
Stefano Pugnetti | 20 Jan 14:40 2015
Picon

node extension: GC tries to free invalid pointer


Hi!

I'm trying to create a node extension to wrap a c library for my
Raspberry PI. The library itself is simple and small, so I just used a
couple of headers to create my interface file. However I had to create a
C++ wrapper class (very simple) to expone a c function of this library
that could not be automatically wrapped by SWIG.

Single calls to the javascript object produced in node work perfectly
fine. However, if I keep calling the object constructor for some time,
the program crashes with messages like:

*** glibc detected *** /usr/local/bin/node: free(): invalid pointer:
0x007f8df1 ***

With gdb I've discovered this always happens during garbage collection:

(gdb) bt
#0  0xb6d178dc in __GI_raise (sig=6) at
../nptl/sysdeps/unix/sysv/linux/raise.c:67
#1  0xb6d1b65c in __GI_abort () at abort.c:92
#2  0xb6d4f9e8 in __libc_message (do_abort=2, fmt=0xb6e035f0 "*** glibc
detected *** %s: %s: 0x%s ***\n")
    at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
#3  0xb6d5a274 in malloc_printerr (action=3, str=0xb6e03738 "free():
invalid pointer", ptr=<optimized out>) at malloc.c:6312
#4  0xb6d5eb18 in __GI___libc_free (mem=<optimized out>) at malloc.c:3738
#5  0xb6c992b8 in _wrap_delete_wrap_wiringPiSPIDataRW (object=...,
parameter=0x7f83d8) at ../sinapsi-wiring-pi_wrap.cxx:4402
#6  0x0032f638 in
v8::internal::GlobalHandles::PostGarbageCollectionProcessing(v8::internal::GarbageCollector)
()
#7  0x00343448 in
v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector,
v8::internal::GCTracer*) ()
#8  0x007ad038 in ?? ()
#9  0x007ad038 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

(The class I implemented is called wrap_wiringPiSPIDataRW).

I don't understand why this happens only after the GC has been destoying
instances of my class for some times (it doesn't happen on the first or
second time -- I found this by setting a breakpoint with gdb); and I
can't figure out what could create the problem. My class is very simple
and I don't use dynamic memory allocation:

class wrap_wiringPiSPIDataRW {
  public:
  wrap_wiringPiSPIDataRW (int channel , int pin);
  int value;
  int exitStatus;
};

wrap_wiringPiSPIDataRW::wrap_wiringPiSPIDataRW (int channel , int pin){
  unsigned char buf[3]={1,0,0};
  buf[1]=(unsigned char)((8+(pin&7))<<4);
  exitStatus = wiringPiSPIDataRW (channel , buf , 3);
  value=buf[2]+(buf[1]&3)*256;
  return;
}

Is this related to a wrong usage of swig? Maybe I should have
implemented a suitable destructor for my class? (Having implemented a
dummy one made no difference)

Please help!

Thanks,

Stefano
Stefano Pugnetti | 20 Jan 09:32 2015
Picon

javascript on node: freeing invalid pointer during GCing

Hi!

I'm trying to create a node extension to wrap a c library for my
Raspberry PI. The library itself is simple and small, so I just used a
couple of headers to create my interface file. However I had to create a
C++ wrapper class (very simple) to expone a c function of this library
that could not be automatically wrapped by SWIG.

Single calls to the javascript object produced in node work perfectly
fine. However, if I keep calling the object constructor for some time,
the program crashes with messages like:

*** glibc detected *** /usr/local/bin/node: free(): invalid pointer:
0x007f8df1 ***

With gdb I've discovered this always happens during garbage collection:

(gdb) bt
#0  0xb6d178dc in __GI_raise (sig=6) at
../nptl/sysdeps/unix/sysv/linux/raise.c:67
#1  0xb6d1b65c in __GI_abort () at abort.c:92
#2  0xb6d4f9e8 in __libc_message (do_abort=2, fmt=0xb6e035f0 "*** glibc
detected *** %s: %s: 0x%s ***\n")
    at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
#3  0xb6d5a274 in malloc_printerr (action=3, str=0xb6e03738 "free():
invalid pointer", ptr=<optimized out>) at malloc.c:6312
#4  0xb6d5eb18 in __GI___libc_free (mem=<optimized out>) at malloc.c:3738
#5  0xb6c992b8 in _wrap_delete_wrap_wiringPiSPIDataRW (object=...,
parameter=0x7f83d8) at ../sinapsi-wiring-pi_wrap.cxx:4402
#6  0x0032f638 in
v8::internal::GlobalHandles::PostGarbageCollectionProcessing(v8::internal::GarbageCollector)
()
#7  0x00343448 in
v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector,
v8::internal::GCTracer*) ()
#8  0x007ad038 in ?? ()
#9  0x007ad038 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

(The class I implemented is called wrap_wiringPiSPIDataRW).

I don't understand why this happens only after the GC has been destoying
instances of my class for some times (it doesn't happen on the first or
second time -- I found this by setting a breakpoint with gdb); and I
can't figure out what could create the problem. My class is very simple
and I don't use dynamic memory allocation:

class wrap_wiringPiSPIDataRW {
  public:
  wrap_wiringPiSPIDataRW (int channel , int pin);
  int value;
  int exitStatus;
};

wrap_wiringPiSPIDataRW::wrap_wiringPiSPIDataRW (int channel , int pin){
  unsigned char buf[3]={1,0,0};
  buf[1]=(unsigned char)((8+(pin&7))<<4);
  exitStatus = wiringPiSPIDataRW (channel , buf , 3);
  value=buf[2]+(buf[1]&3)*256;
  return;
}

Is this related to a wrong usage of swig? Maybe I should have
implemented a suitable destructor for my class? (Having implemented a
dummy one made no difference)

Please help!

Thanks,

Stefano

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
Stefano Pugnetti | 19 Jan 17:11 2015
Picon

javascript on node: freeing invalid pointer during GCing

Hi!

I'm trying to create a node extension to wrap a c library for my
Raspberry PI. The library itself is simple and small, so I just used a
couple of headers to create my interface file. However I had to create a
C++ wrapper class (very simple) to expone a c function of this library
that could not be automatically wrapped by SWIG.

Single calls to the javascript object produced in node work perfectly
fine. However, if I keep calling the object constructor for some time,
the program crashes with messages like:

*** glibc detected *** /usr/local/bin/node: free(): invalid pointer:
0x007f8df1 ***

With gdb I've discovered this always happens during garbage collection:

(gdb) bt
#0  0xb6d178dc in __GI_raise (sig=6) at
../nptl/sysdeps/unix/sysv/linux/raise.c:67
#1  0xb6d1b65c in __GI_abort () at abort.c:92
#2  0xb6d4f9e8 in __libc_message (do_abort=2, fmt=0xb6e035f0 "*** glibc
detected *** %s: %s: 0x%s ***\n")
    at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
#3  0xb6d5a274 in malloc_printerr (action=3, str=0xb6e03738 "free():
invalid pointer", ptr=<optimized out>) at malloc.c:6312
#4  0xb6d5eb18 in __GI___libc_free (mem=<optimized out>) at malloc.c:3738
#5  0xb6c992b8 in _wrap_delete_wrap_wiringPiSPIDataRW (object=...,
parameter=0x7f83d8) at ../sinapsi-wiring-pi_wrap.cxx:4402
#6  0x0032f638 in
v8::internal::GlobalHandles::PostGarbageCollectionProcessing(v8::internal::GarbageCollector)
()
#7  0x00343448 in
v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector,
v8::internal::GCTracer*) ()
#8  0x007ad038 in ?? ()
#9  0x007ad038 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

(The class I implemented is called wrap_wiringPiSPIDataRW).

I don't understand why this happens only after the GC has been destoying
instances of my class for some times (it doesn't happen on the first or
second time -- I found this by setting a breakpoint with gdb); and I
can't figure out what could create the problem. My class is very simple
and I don't use dynamic memory allocation:

class wrap_wiringPiSPIDataRW {
  public:
  wrap_wiringPiSPIDataRW (int channel , int pin);
  int value;
  int exitStatus;
};

wrap_wiringPiSPIDataRW::wrap_wiringPiSPIDataRW (int channel , int pin){
  unsigned char buf[3]={1,0,0};
  buf[1]=(unsigned char)((8+(pin&7))<<4);
  exitStatus = wiringPiSPIDataRW (channel , buf , 3);
  value=buf[2]+(buf[1]&3)*256;
  return;
}

Is this related to a wrong usage of swig? Maybe I should have
implemented a suitable destructor for my class? (I implemented a dummy
one, but nothing changed)

Please help!

Thanks,

Stefano

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
theand | 18 Jan 18:57 2015

SWIG and C++11 template aliases in policy classes

Hi all, I am having some difficulty with SWIG 3.0.4 parsing lines such as the following in C++11:

typedef typename Policy::template PolicyTemplate< T > InternalTemplate;

I am hoping someone on the list can provide some suggestions, help me construct some alternative
solutions, or inform me that what I am trying to do is not possible.  :)  Here is some sample code to reproduce
this problem.   Note that both clang and gcc accept this syntax, and produce code.  SWIG tells me I have some
sort of erroneous input:

--Begin Experiment.h--

#ifndef EXPERIMENT_H
#define EXPERIMENT_H

template< typename T >
class MyTemplate {
    T m_MyTemplateVariable;
};

struct Policy {
    template< typename T >
    using PolicyTemplate = MyTemplate<T>;
};

template< typename Policy, typename T >
class TestClass {
    public:
        typedef typename Policy::template PolicyTemplate< T > InternalTemplate;
        TestClass() {}
    private:
        InternalTemplate m_TestMember;
};

#endif

--End Experiment.h--

--Begin Experiment.cpp--

#include "Experiment.h"

int main( int argc, char *argv[] ) {
    TestClass< Policy, int > inttest;
    TestClass< Policy, float > floattest;

    return 0;
}

--End Experiment.cpp--

And in Experiment.i, I have this:

--Begin Experiment.i--

%module Experiment
%{

#include "Experiment.h"

%}

%include "Experiment.h"

%template( IntTestClass ) TestClass< Policy, int >;
%template( FloatTestClass ) TestClass< Policy, float >;
When I compile, I use c++ -o Experiment -std=c++11 Experiment.cpp. The code compiles. When I run swig, I get:

Experiment.h:11: Warning 342: The 'using' keyword in template aliasing is not fully supported yet.
Experiment.h:17: Error: Syntax error in input(3).

--End Experiment.i--

When I compile Experiment.cpp with C++11, the code compiles fine.  When SWIG runs, I get two lines of output:

Experiment.h:11: Warning 342: The 'using' keyword in template aliasing is not fully supported yet.
Experiment.h:17: Error: Syntax error in input(3).

I expect the first line, based on the documentation.  I don't expect the second line, or at least the
documentation doesn't hint at this sort of syntax being a problem.

Any assistance would be greatly appreciated.

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
Albert Chin | 17 Jan 15:59 2015

Replacing SWIG-generated Python wrapper with custom wrapper

I'm trying to wrap the following function using SWIG for Python:
  void archive_read_data_set_progress_callback(struct archive *_a,
      void (*progress_func)(void *), void *user_data)

According to http://www.swig.org/Doc2.0/SWIGDocumentation.html, 5.4.9
("Pointers to functions and callbacks"):
  Although SWIG does not normally allow callback functions to be written
  in the target language, this can be accomplished with the use of
  typemaps and other advanced SWIG features. See the Typemaps chapter
  for more about typemaps and individual target language chapters for
  more on callbacks and the 'director' feature.

I have this mostly working. How do I replace the SWIG-generated
wrapper for this function with a custom wrapper?

I've taken the cue for my solution from http://tinyurl.com/pmmnbxv.
I'v added the following:
  %{
  static __thread PyObject *callback;
  static void dispatcher (void *user_data) {
    PyObject_CallFunctionObjArgs(callback, user_data, NULL);
  }
  %}

  %typemap(in) void (*)(void *) {
    if (!PyCallable_Check($input)) SWIG_fail;
    $1 = dispatcher;
    callback = $input;
  }

The problem with this is that I need to pass obj2 directly to
archive_read_data_set_progress_callback() without unwrapping it (thus
passing the Python object directly to the callback function in
dispatcher() above). So, I need to override how SWIG passed obj2 by
replacing:
  res3 = SWIG_ConvertPtr(obj2,SWIG_as_voidptrptr(&arg3), 0, 0);
  if (!SWIG_IsOK(res3)) {
    SWIG_exception_fail(SWIG_ArgError(res3), "in method '"
"archive_read_data_set_progress_callback" "', argument " "3"" of type '" "void *""'"); 
  }
  archive_read_data_set_progress_callback(arg1,arg2,arg3);
with just:
  archive_read_data_set_progress_callback(arg1,arg2,obj2);

So, I plan to just replace the entire
_wrap_archive_read_data_set_progress_callback() function with one of
my own creation.

The other problem with this solution is that I can essentially have
only one callback because of the following code above.
  static __thread PyObject *callback;
How would I solve this?

--

-- 
albert chin (china <at> thewrittenword.com)

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
Orhun Birsoy | 15 Jan 18:32 2015
Picon

Ruby SWIG module definition

Hi,

I would like to put SWIG module defined in SWIG_Ruby_InitRuntime under
some other module.

In order to achieve this I made the following changes in rubyrun.swg,

#if !defined(MYSWIGEXT_USE_SWIGPARENT)
  static VALUE _mSWIGModuleParent = Qnil;
#endif

/* Initialize Ruby runtime support */
SWIGRUNTIME void
SWIG_Ruby_InitRuntime(void)
{
  if (_mSWIG == Qnil) {
    if(_mSWIGModuleParent != Qnil) {
      _mSWIG = rb_define_module_under(_mSWIGModuleParent, "SWIG");
    } else {
      _mSWIG = rb_define_module("SWIG");
    }

    swig_call_id  = rb_intern("call");
    swig_arity_id = rb_intern("arity");
  }
}

In my case, _mSWIGModuleParent declared and initialized generic enough
for my needs.

Although all my tests succeeded, I would like to make sure...

1, Is there another way to change how SWIG module is defined?
2. Can SWIG module needs to be global or can it live under another module?

Thanks.

--

-- 
Orhun Birsoy

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
Joel Andersson | 14 Jan 15:41 2015
Picon

Wrapping friend function

Hello,

I am trying to wrap a friend function as a inside a template class:

namespace casadi {
  template<typename T>
  class Matrix {
      ...
      friend inline Matrix<T> mtimes(const Matrix<T> &x, const Matrix<T> &y) { ... }
  };
}

This declaration allows the function to used together with argument dependent lookup (ADL) and implicit type conversion. But if I try to wrap this function with SWIG, a warning appears saying that the function will be ignored.

The only solution that I found working was declaring a global function in SWIG with the same signature:

Matrix<T> mtimes(const Matrix<T> &x, const Matrix<T> &y);

for every DataType. Of course there isn't any global function like that in reality, but I didn't manage to %rename the function to a function in global namespace. And mtimes is not a member of the casadi namespace (since we're using ADL).

Is there any better way of solving this? 

Best regards,
Joel

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Pawel Tomulik | 7 Jan 00:26 2015
Picon
Picon

Unignore all and %template

Hi,

I have this example:

   // test.i
   %module test
   // %rename("$ignore","%$isfunction") ""; // .. or such not important
   %rename("%s") "";
   %inline { template <class T> struct X {}; }
   %template(Xi) X<int>;

If try to compile, I get a warning and template is not specialized:

swig -python -builtin -enumclass -c++ -o test_wrap.cc test.i
test.i:6: Warning 503: Can't wrap class X< int > unless renamed to a 
valid identifier.

This is caused by the %rename("%s") ""; Is this normal? How should I 
properly un-ignore all symbols?

Regards!
--

-- 
Paweł Tomulik

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Gmane