brainstorm | 18 Aug 18:49 2014
Picon

SWIG python segfault on OSX, fine on Linux

Sorry for the cross-posting, but I wonder if any of you have a good solution for this problem I'm experiencing with SWIG and Python on OSX:

http://stackoverflow.com/questions/25349828/barebones-swig-python-c-interface-segfaulting-on-osxclang-not-in-linuxgcc

Thanks in advance!
Roman
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 18 Aug 10:22 2014

Can anyone tell me how to do this simple thing in Swig ?

Hi,

I posted what was going wrong for me with Swig but got no reply.  My post was rather long and what I really want to know is given the following code in C :

     typdef struct
    {
        int * i1;
        char* c1;
        int len;
    } MY_STRUCT;
   
    bool myfunc (MY_STRUCT* pms)
    {
        strncpy (pms->c1, "Hello from the world of Swig", pms->len);
        *pms->i1 = strlen(pms->c1);
    }

What do I need to put in a .i file and what java code in a .java file in order to be able to have read and write access to java data in my C code ? (I.e. read and write through pointers ?).

My .i file is effectively the same as what follows below and if anyone can tell me why I get flakey problems when I write to the i1 and c1 elements of MY_STRUCT thenm I would be grateful.  I am using Swig Version 3.0.2 on Mac OS X Mavericks.

Many thanks,

Mike


/* File : MY_STRUCT.i */
%module MY_STRUCT

// Enable the JNI class to load the required native library.
%pragma(java) jniclasscode=%{
 static {
 try {
 java.lang.System.loadLibrary("MY_STRUCT");
 } catch (UnsatisfiedLinkError e) {
 java.lang.System.err.println("native code library failed to load.\n" + e);
 java.lang.System.exit(1);
 }
 }
%}

%include <typemaps.i>
%apply byte * INOUT {char*};
%apply int * INOUT {int*};

typdef struct
{
    int * i1;
    char* c1;
    int len;
} MY_STRUCT;

%include cpointer.i

bool myfunc (MY_STRUCT* pms)
{
    strncpy (pms->c1, "Hello from the world of Swig", pms->len);
    *pms->i1 = strlen(pms->c1);
}

%pointer_functions(int, intp);

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 14 Aug 12:53 2014

Java, Android, Swig structure with pointers problem.

Hi,

I made a development but I don't really know what it means.  If I change the line

    %apply signed char * INOUT {char*};
    
to

    %apply byte * INOUT {char*};

my Swig invocation gives the following error :
    
    Warning 453: Can't apply (byte *INOUT). No typemaps are defined.

However the generated code compiles and runs OK with the proviso that I need to change my java application code to use a String instead of a byte[] for its version of c1.

My new java code is :

    <at> Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    /*  typedef struct
        {
                int* i1;
                char* c1;
                int len;
        }*P_MYSTRUCT, MYSTRUCT;*/
        S_MESSAGE_STRUCT p_my_struct = new S_MESSAGE_STRUCT();
       
        int i1 = 1;
        SWIGTYPE_p_int i1p = Message.new_intp();
        Message.intp_assign(i1p, i1);

        int len = 128;
        byte[] c1 = new byte[len];
        
        String s1 = new String(c1);
        p_my_struct.setI1(i1p);
        p_my_struct.setC1(s1);
        p_my_struct.setLen(len);
        
        Message.t_func(p_my_struct);
        
        SWIGTYPE_p_int pi1_ret = p_my_struct.getI1();
        Integer i1_ret = Message.intp_value(pi1_ret);
        String display = p_my_struct.getC1();

        display += i1_ret;

        final EditText eText = (EditText) findViewById(R.id.editText1);

        eText.setText(display);
    }

Does this shed any light on what I may be doing wrong ?

Any ideas gratefully received,

Mike
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 13 Aug 15:43 2014

Java, Android, Swig structure with pointers problem.

Hi,

Apologies in advance for such a long first missive but I am trying to use Swig to generate wrappers for some in-house C-code so we can reuse it for a new Android java project and am having problems.  I am new to both Java and Swig so please be gentle with me in terms of the technical content of any replies.

I am trying to wrap a C-structure called S_MESSAGE_STRUCT which contains pointers in such a way that its elements can be accessed (set up, written, read etc) from java and also by legacy C code.  My C header file defining the struct is as follows, the code is exemplary and simplified from my real code but it displays the same problems :

    #ifndef MESSAGE_H_
    #define MESSAGE_H_
   
    #include <stdbool.h>

    typedef struct
    {
        int*    i1;
        char*   c1;
        int        len;
    } *P_S_MESSAGE_STRUCT, S_MESSAGE_STRUCT;
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct);
   
    #endif /* ndef MESSAGE_H_ */

My C file contains a single function which tests the int* i1 element of the S_MESSAGE_STRUCT and then writes to the c1 and i1 elements as follows :

    #include "message.h"
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct)
    {
        if (*p_s_mystruct->i1 == 1)
        {
            strcpy(p_s_mystruct->c1, "Hello from swig");
            p_s_mystruct->c1[p_s_mystruct->len-1] = '\0';
            *p_s_mystruct->i1 = strlen(p_s_mystruct->c1);
        }
       
        return true;
    }

I am using a .i file as follows :

    /* File : Message.i */
    %module Message
    %{
    /* Includes the header in the wrapper code */
    #include "../../../common/message/message.h"
    %}
   
    // Enable the JNI class to load the required native library.
    %pragma(java) jniclasscode=%{
     static {
     try {
     java.lang.System.loadLibrary("Message");
     } catch (UnsatisfiedLinkError e) {
     java.lang.System.err.println("native code library failed to load.\n" + e);
     java.lang.System.exit(1);
     }
     }
    %}

    %include <typemaps.i>
    %apply signed char * INOUT {char*};
   
    typedef struct
    {
        int*      i1;
        char*   c1;
        int       len;
    } *P_S_MESSAGE_STRUCT, S_MESSAGE_STRUCT;
   
    %include cpointer.i
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct);
   
    %pointer_functions(int, intp);

My java source function is as follows (edited from a default Android Activity) :

    <at> Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
    /*  typedef struct
        {
                int* i1;
                char* c1;
                int len;
        }*P_MYSTRUCT, MYSTRUCT;
    */
        S_MESSAGE_STRUCT p_my_struct = new S_MESSAGE_STRUCT();
      
        int i1 = 1;
        SWIGTYPE_p_int i1p = Message.new_intp();
        Message.intp_assign(i1p, i1);

        int len = 128;
        byte[] c1 = new byte[len];
       
        p_my_struct.setI1(i1p);
        p_my_struct.setC1(c1);
        p_my_struct.setLen(len);
       
        Message.t_func(p_my_struct);
        Integer i1_ret = Message.intp_value(i1p);
       
        byte[] ret_c1 = p_my_struct.getC1();
       
        String display = new String();
    // !!        display = Arrays.toString(ret_c1); // <<<< Gives Exception !!
        display += i1_ret;

        final EditText eText = (EditText) findViewById(R.id.editText1);

        eText.setText(display);
    }

Everything compiles and builds OK and there are no errors from the swig invocation of :
      
    swig -java -package com.mobbu.Message -outdir ../../src/com/mobbu/Message/ -verbose Message.i
   
but I know that I am doing something wrong because I keep getting flakey problems after I call the t_func() function from java.  The problems are various and include the program hanging after the call to t_func() completes but sometimes the program runs and displays an output OK.  When output is displayed the contribution due to c1 is null (it just gives output "15" with no signs of "Hello from swig".

I have determined that the program flakiness stems from the writes to the pointer elements i1 and c1 in the P_MYSTRUCT struct, because there are no problems either without those elements or without the writes (but leaving the c1,i1 elements in the struct).

I would be very grateful for help on how to achieve my goal of being able to use pointer elements of C structs and for any ideas of what I am doing wrong in my Message.i file ?  I am using SWIG Version 3.0.2.
Thanks,

Mike
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Taylor Centers | 13 Aug 16:31 2014
Picon

Just starting SWIG question

I have a c++ static library file (.lib) that I would like to generate an interface for to c#, I've been able to get the imports and the function definitions out of it and have done my best at making the interface file for the library, and have run "swig -c++ -csharp interface.i" and it generates a lot of files for me, a lot of .cs and one .cxx.

I guess my questions are: have I done these first steps correctly? and, Where do I go from here?
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
natarajan manikandan | 11 Aug 15:13 2014
Picon

Could not able to write the proper interface file for Csharp


Hi Swig users,
 
I could not able write the proper interface file when my object factory returns via boost:: shared_ptr as shown below
//MyPackFactory.h
namespace MyPack{
class  ATPACK_API MyPackServiceFactory
{
public:    boost::shared_ptr<IMyPackService> getPackService();
};
}
//MyPackServices.h
namespace MyPack{
//Represents information 
class MyPackService : public IMyPackService
{
public:
MyPackService(){}
    virtual std::vector<std::string> getPacksList();
};}
//MyPackInterfaceServices.h
namespace MyPack{
class IMyPackService 
{
public:
    virtual std::vector<std::string> getPacksList() = 0; };
}

My usage would be in cpp file is like this:
//Main.cpp
 MyPack::MyPackServiceFactory MyPackFactory;
    boost::shared_ptr<MyPackPack::IMyPackService> service = MyPackFactory.getPackService();
    std::vector<std::string> packlist = service->getPacksList();


to consume the same in C# project by creating wrapper using swig, what I should do?I have tried my attempt like this below:
\\mypack.i
%module MyPackWrap
%{
#include "MyPackServiceFactory.h"
#include "MyPackInterfaceService.h"
#include "boost\shared_ptr.hpp"
#include "MyPackService.h"
%}
// Some required SWIG headers
%include <windows.i>
%include <std_vector.i>
%include <std_string.i>
%include "MyPackInterfaceService.h"
%include "MyPackService.h"
%include "MyPackServiceFactory.h"
%include <boost_shared_ptr.i>
%template(vectorofstring) std::vector<std::string>;
%shared_ptr(atPackService)
%shared_ptr(IAtPackService)
%shared_ptr(AtPackServiceFactory)

Could anyone help me?
Best Regards,
 
Manikandan N
9980007590
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Umberto Ferraro Petrillo | 10 Aug 09:04 2014
Picon

Passing multidimensional int array from Java to C (and back)

Hi all, I need to invoke from Java a C function that takes, as input, a bidimensional int array. The same array is used by the C function to provide its output. I modeled the bidimensional array as an array of int* using the "array_functions" directive available with SWIG:

%array_functions(int*, intArray);

As expected, SWIG generates the SWIGTYPE_p_p_int type plus following two helper functions:

public static int[] intArray_getitem(SWIGTYPE_p_p_int ary, int index) { return contrastJNI.intArray_getitem(SWIGTYPE_p_p_int.getCPtr(ary), index); } public static void intArray_setitem(SWIGTYPE_p_p_int ary, int index, int[] value) { contrastJNI.intArray_setitem(SWIGTYPE_p_p_int.getCPtr(ary), index, value); }

I am able to allocate from Java the SWIGTYPE_p_p_int class and to write into it. However, as soon as I try (from Java) to access the content of the array (like in the following example), I get a segmentation fault exception:

SWIGTYPE_p_p_int abc = contrast.new_intArray(1000); contrast.intArray_setitem(abc, 1, new int[100]); // invoke the C function passing it 'abc' int[] def = contrast.intArray_getitem(abc, 1);

As far I can see, the C function is able to read and write 'abc', without any problem. Thus I tried a second code, where I took off the C part, and I still get the same error:

SWIGTYPE_p_p_int abc = contrast.new_intArray(1000); contrast.intArray_setitem(abc, 1, new int[100]); int[] def = contrast.intArray_getitem(abc, 1); Is there anybody who can help me with this?

Thank you in advance!

Note: I posted the same question on stackoverflow a couple of weeks ago (see http://stackoverflow.com/questions/25139171/passing-multidimensional-int-array-from-java-to-c-and-back-using-swig)

--
Umberto Ferraro Petrillo
PhD
Dipartimento di Scienze Statistiche
Universita' di Roma -- La Sapienza
P.le Aldo Moro, 5
00185 - Rome, Italy
phone: +39 06 499 10513
e-mail: Umberto.Ferraro <at> uniroma1.it
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
shekhar | 9 Aug 08:13 2014

load shared library (.so) in ecilipswe luna or netbeans

Hello all,
  • hello all, I am new to java , eclipse and netbeans ,my task was to convert c legacy code to java i completed that task using swig 3.0 i created a shared libraray in (.so) in linux using gcc 4 version example=libxyz.so now with command line i am able to run java file and get the output there the library loading is easy but in eclipse and netbeans i dont know how to do that please explain me in steps of importing (libxyz.so) in eclipse and netbeans (eclipse luna) and (netbeans 8.0) in linux.

thanks

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Brendan Le Foll | 6 Aug 16:14 2014
Picon

Inserting code at the end of the %init section

Hi all,

I'm working on a library where I have a nasty hack to do callbacks
into the scripting language (python & javascript). I managed to do
what I want but I need to insert some code at the end of the %init
routine.

To explain a little further I want this:

void SWIGV8_INIT (v8::Handle<v8::Object> exports,
v8::Handle<v8::Object> /*module*/)
{
// some generated code

// my %init section
mraa_init();

// some generated code

// what I want to insert
SWIGV8_AddMemberFunction(....);
}
// post-init

Basically I'm struggling with %extend (not too sure why), so was
thinking I could get away with just registering the member function
myself and using %native. This works but I need to modify the wrapper
file which is no good. I tried using %insert("post-init") but that
ends up being out of the SWIGV8_INIT function.

Any ideas?
Brendan

------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
Paolo Greppi | 30 Jul 13:57 2014

swig/python memory leak on function returning a boost::gregorian::date object

I build this toy wrapper around a function returning a
boost::gregorian::date object:

swig -I. -python test.sw
g++ -std=c++11 -c -o test.o test.cc
g++ -I/usr/include/python2.7 -c -o test_wrap.o test_wrap.c
gcc -shared -fpic test.o test_wrap.o -lstdc++ -o _test.so

When I run the actual test:
./testtest.py

I get this error: "swig/python detected a memory leak of type
'boost::gregorian::date *', no destructor found"

How do I know if SWIG is really leaking memory ? How do I get rid of the
message ?

TIA, Paolo

P.S. Tried on Debian testing (boost 1.55, swig 2.0.12 and python 2.7.8)

==== test.h
#include <boost/date_time/gregorian/gregorian_types.hpp>
boost::gregorian::date date(int year, int month, int day);

==== test.cc
#include "test.h"
boost::gregorian::date date(int year, int month, int day) {
  boost::gregorian::date date(year, month, day);
  return date;
}

==== test.sw
%module test
%{
#include "test.h"
%}
%include "test.h"

==== testtest.py
#!/usr/bin/env python
import test
test.date(2014,1,1)

------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
Vijay Krishnan | 30 Jul 08:01 2014
Picon

regarding using SWIGTYPE_p_p_char in java

Hi,

      I am using array of constant pointer to char pointer in c.When i use SWIG to generate JNI code for this,it creates SWIGTYPE_p_p_char.java wrapper file.How to make use of this SWIGTYPE_p_p_char ?

Regards,
Vijay K
------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Gmane