Ling Li | 11 Feb 07:04 2016
Picon

SWIG Python: %attributeval and std::vector don't work together?

Hello,

I wonder if anyone can give an example of using %attribute (%attributeval) with std::vector.  I somehow always get an opaque SwigObject (instead of Python tuple), and in addition there's memory leak.

Here's my simple example:

$ cat example.h example.i runme.py
/* File : example.h */
#include <vector>
struct Shape {
  std::vector<double> values() {
      std::vector<double> v;
      v.push_back(3);
      return v;
  }
};

/* File : example.i */
%module example
%include <attribute.i>
%include <std_vector.i>
%{
#include "example.h"
%}
%attributeval(Shape, std::vector<double>, values, values);
%include "example.h"
%template() std::vector<double>;

# file: runme.py
import example
s = example.Shape()
print s.values

Here are the output:

$ swig -python -c++ example.i
$ g++ -shared -fPIC -I /usr/include/python2.7/ -o _example.so example_wrap.cxx
$ python runme.py
<Swig Object of type 'std::vector< double,std::allocator< double > > *' at 0x7f4a42939090>swig/python detected a memory leak of type 'std::vector< double,std::allocator< double > > *', no destructor found.

The swig version is 3.0.8.

Note that this question was also asked in this 2014 post: https://sourceforge.net/p/swig/mailman/message/32245826/

Any thoughts or suggestions would be highly appreciated.  Thanks!

Ling
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
David Burns | 6 Feb 00:59 2016
Picon

swig macros for types with spaces eg unsigned short

Dear swig users, If anyone could help me out with this I would be much obliged. Question is how to use a swig macro with a type with two words: eg unsigned short TEMPLATE_HELPER(unsigned short,2) TEMPLATE_HELPER(unsigned short,3) Those lines give me a syntax error. I have tried using typedef unsigned short ushort This prevents the syntax error from occurring, and the code compiles but when loading the module I've found the %apply directives for ushort didn't work. The only solution that I got to work was using a template and apply directive for the unsigned short outside the macro. Your suggestion on how to handle this would be welcome. Thanks David %module pImg %{ #define SWIG_FILE_WITH_INIT #include "pImg.h" #include "pImg.cpp" %} %include "numpy.i" %init %{ import_array(); %} template<typename PixelType,unsigned int Dim> class pImg { public: typedef double CoordType; // constructor/destructor pImg(); ~pImg(); // python interface inputs void SetScalars(PixelType * scalars, int sz1, int sz2, int sz3); void SetInputSpacing(CoordType* spacing, int sz); void SetInputOrigin(CoordType* origin, int sz); // python interface outputs void CopyOutput(PixelType* scalars_cpout, int sz1, int sz2, int sz3); void GetOutputPtr(PixelType** scalars_out, int* sz1, int* sz2, int* sz3); void GetOutputSpacing(CoordType spacing_out[3]); void GetOutputOrigin(CoordType origin_out[3]); // example void MagGrad(); }; %define TEMPLATE_HELPER(PixType,ImgDim) %template(pImg_ ## PixType ## ImgDim) pImg<PixType,ImgDim>; %apply (PixType * IN_ARRAY3, int DIM1, int DIM2, int DIM3){(PixType * scalars, int sz1, int sz2, int sz3)} %apply (PixType * INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3){(PixType * scalars_cpout, int sz1, int sz2, int sz3)} %apply (PixType ** ARGOUTVIEW_ARRAY3, int* DIM1, int* DIM2, int* DIM3){(PixType ** scalars_out, int* sz1, int* sz2, int* sz3)} %enddef // SWIG macro expansion to force instantiation of relevant types, currently only 2D/3D images supported // Comment out irrelevant types for faster compiling TEMPLATE_HELPER(short,2) TEMPLATE_HELPER(short,3) TEMPLATE_HELPER(unsigned short,2) TEMPLATE_HELPER(unsigned short,3) TEMPLATE_HELPER(float,2) TEMPLATE_HELPER(float,3) TEMPLATE_HELPER(double,2) TEMPLATE_HELPER(double,3) %apply (double* IN_ARRAY1, int DIM1) {(double* spacing, int sz),(double* origin, int sz)} // Generic typemap for outputting on return an array of doubles %typemap(in,numinputs=0) double[ANY] (double temp[$1_dim0]) { memset(temp, 0, sizeof temp); $1 = temp; } %typemap(argout) double[ANY] { int i; $result = PyList_New($1_dim0); for (i = 0; i < $1_dim0; i++) { PyObject *o = PyFloat_FromDouble((double) temp$argnum[i]); PyList_SetItem($result,i,o); } }
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Andrew Haining | 5 Feb 11:51 2016

Re: Method chaining and inter object dependencies in python

The only way to do it without changing the C++ code is to prevent the C++ ‘C’ object from being deleted, the only way to do that is to prevent the python ‘C’ object from being destroyed by the garbage collector.

I can see how C++ D() could be wrapped via swig code with a python object that takes a ‘C’ as a parameter, this will mean that ‘C’ will not be cleaned up by the garbage collector until all references to its ‘D’ are cleaned up.

This seems like a lot of work to avoid writing a C++ wrapper for your library imho but it does seem possible to do.

 

From: Martin Siggel [mailto:martinsiggel <at> gmail.com]
Sent: 05 February 2016 09:53
To: Andrew Haining <andrew.haining <at> digitalbarriers.com>
Subject: Re: [Swig-user] Method chaining and inter object dependencies in python

 

Thanks for you answer andrew.


Unfortunately changing the C++ API is not an option. It is a thirdparty library which I can't change.

Also, my demo code is extremely simplified. The real library already uses a custom kind of smart pointers. The problem still exists, as often, the smart pointer is returned by reference instead of by value.

Therefore, I need a swig-only solution to bypass the API problems. Is there any other way than changing the C++ code?

 

2016-02-05 10:20 GMT+01:00 Andrew Haining <andrew.haining <at> digitalbarriers.com>:

There are many ways to do this, depending on specifics of the use case etc. The first thing I’d say is swig is behaving exactly as it should and your problem is with the garbage collector of your managed language. The easiest way to achieve what you’re looking for is to change GetD() to return a copy, not a reference, this will give the D object its own unique entry in the garbage collectors reference table and guarantee it stays in scope as long as a managed programmer expects. If you truly do want a reference to the actual D inside C and not a copy then I would recommend passing a std::shared_ptr<D> from GetD. ( I personally use the pimpl idiom and so pass a wrapper object around a std::shared_ptr<D> but you can also pass the shared_ptr<D> directly) C++ shared pointers aren’t compatible with raw memory management so this will involve changing all the management of D’s memory (no new’s, no deletes).

Hope that helps.

 

From: Martin Siggel [mailto:martinsiggel+swig <at> gmail.com]
Sent: 05 February 2016 08:56
To: swig-user <at> lists.sourceforge.net
Subject: [Swig-user] Method chaining and inter object dependencies in python

 

Suppose I have an object C that serves as a container for another D.

If I return the contained object D, how can I tell swig/python,
that the returned object D depends on the container and the container C
must not be deleted as long as D exists?


Heres a minimal example demonstrating the issue I have.


The C++ code is as simple as:


class D

{

public:

    D() {v = 4;}

    int GetValue() {return v; }

 

private:

    int v;

};

 

class C

{

public:

    C() {d = new D();}

    ~C() {delete d;}

 

    D* GetD() { return d; }

private:

    D* d;

};

The following python code demonstrates the problem:

# problematic python code

print C().GetD().GetValue()

>>> 7036080

In this basic chaining example, C() gets garbage collected
directly after the call to GetD(). This also deletes the
contained object d and makes the call to GetValue invalid.

If we don't chain the call, we make sure that c is notdeleted too
early and the example works:

# this is working on the other hand

c = C()

c.GetD().GetValue()

>>> 4

Is there a way, to tell Swig, that D depends on C after calling GetD()?

This can be probably done using the reference counting mechanism,
but I don't know how.

Martin


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

 

Digital Barriers e-Mail Confidentiality and Disclaimer

                   

This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

 

Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.

 


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________




--

Dr. Martin Siggel

Neusser Straße 257

50733 Köln 

Phone: 0178 6729772


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Martin Siggel | 5 Feb 09:56 2016
Picon

Method chaining and inter object dependencies in python

Suppose I have an object C that serves as a container for another D.
If I return the contained object D, how can I tell swig/python,
that the returned object D depends on the container and the container C
must not be deleted as long as D exists?

Heres a minimal example demonstrating the issue I have.


The C++ code is as simple as:

class
D { public: D() {v = 4;} int GetValue() {return v; } private: int v; }; class C { public: C() {d = new D();} ~C() {delete d;} D* GetD() { return d; } private: D* d; };

The following python code demonstrates the problem:

# problematic python code print C().GetD().GetValue() >>> 7036080

In this basic chaining example, C() gets garbage collected
directly after the call to GetD(). This also deletes the
contained object d and makes the call to GetValue invalid.
If we don't chain the call, we make sure that c is notdeleted too
early and the example works:

# this is working on the other hand c = C() c.GetD().GetValue() >>> 4

Is there a way, to tell Swig, that D depends on C after calling GetD()?
This can be probably done using the reference counting mechanism,
but I don't know how.

Martin
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Jimi Damon | 3 Feb 22:19 2016
Gravatar

How to prevent allocation and wrapping of a return type

Hi,

I have created a typedef of a 64 bit integer that will be my return type 
called AIORET_TYPE.
ie:

typedef int64_t AIORET_TYPE.

I have a number of functions that return this AIORET_TYPE type. Hence I 
don't want to create custom signatures for all of these functions.

My problem is that SWIG believes that this is a structure, is allocating 
memory ( of size AIORET_TYPE ) to copy this value into , and it is 
returning as a PyObject *.

How can I get SWIG to ignore this type and realize that nothing needs to 
be allocated ?

I've tried adding

typedef int64_t AIORET_TYPE;

and

%inline %{
typedef int64_t AIORET_TYPE;
%}

but neither have worked for me and I still see this error message:

swig/python detected a memory leak of type 'AIORET_TYPE *', no 
destructor found.

This seems simple but I can't find a good place to start in the 
documentation.

-Jimi

-- 
WARNING - This e-mail or its attachments may contain controlled technical 
data or controlled technology within the definition of the International 
Traffic in Arms Regulations (ITAR) or Export Administration Regulations 
(EAR), and are subject to the export control laws of the U.S. Government. 
Transfer of this data or technology by any means to a foreign person, 
whether in the United States or abroad, without an export license or other 
approval from the U.S. Government, is prohibited. The information contained 
in this document is CONFIDENTIAL and property of ACCES I/O Products, Inc. 
Any unauthorized review, use, disclosure or distribution is prohibited 
without express written consent of ACCES I/O Products, Inc. If you are not 
the intended recipient, please contact the sender and destroy all copies of 
the original message and enclosed attachments.

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
Jimi Damon | 3 Feb 22:35 2016
Gravatar

How to prevent allocation and wrapping of a return type

Hi,

I have created a typedef of a 64 bit integer that will be my return type 
called AIORET_TYPE.
ie:

typedef int64_t AIORET_TYPE.

I have a number of functions that return this AIORET_TYPE type. Hence I 
don't want to create custom signatures for all of these functions.

My problem is that SWIG believes that this is a structure, is allocating 
memory ( of size AIORET_TYPE ) to copy this value into , and it is 
returning as a PyObject *.

How can I get SWIG to ignore this type and realize that nothing needs to 
be allocated ?

I've tried adding

typedef int64_t AIORET_TYPE;

and

%inline %{
typedef int64_t AIORET_TYPE;
%}

but neither have worked for me and I still see this error message:

swig/python detected a memory leak of type 'AIORET_TYPE *', no 
destructor found.

This seems simple but I can't find a good place to start in the 
documentation.

-Jimi

-- 
WARNING - This e-mail or its attachments may contain controlled technical 
data or controlled technology within the definition of the International 
Traffic in Arms Regulations (ITAR) or Export Administration Regulations 
(EAR), and are subject to the export control laws of the U.S. Government. 
Transfer of this data or technology by any means to a foreign person, 
whether in the United States or abroad, without an export license or other 
approval from the U.S. Government, is prohibited. The information contained 
in this document is CONFIDENTIAL and property of ACCES I/O Products, Inc. 
Any unauthorized review, use, disclosure or distribution is prohibited 
without express written consent of ACCES I/O Products, Inc. If you are not 
the intended recipient, please contact the sender and destroy all copies of 
the original message and enclosed attachments.

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
Álvaro Hernández Tortosa | 2 Feb 02:09 2016

SWIGing PostgreSQL's SPI interface

     Hi list.

     I'm a newcomer to Swig.

     I'm trying to generate Java classes for PostgreSQL's SPI (Server 
Programming Interface), an internal API used by PostgreSQL to execute 
queries. It is defined in a single .h file:

server/executor/spi.h        (available within PostgreSQL's source code 
or by installing the postgresql-dev package or similar)

     When running swig, I get the following error:

/usr/include/postgresql/9.5/server/executor/spi.h:62: Error: Syntax 
error - possibly a missing semicolon.

     The command executed and the .i file are:

swig -java -package out -outdir out spi.i

%module pq
%{
#include "/usr/include/postgresql/9.5/server/executor/spi.h"
%}

%include "/usr/include/postgresql/9.5/server/executor/spi.h"

     Needless to say, the .h seems to be correct (non-modified from 
PostgreSQL 9.5) and compiles fine. PostgreSQL is strict C. So I guess 
source code is not the problem ;P

     I have also tried to add to spi.i include lines for the .h files 
included by spi.h, with lines similar as the ones above, but the error 
is still the same. I tried with a different PostgreSQL include file 
(libpq-fe.h) and worked like a charm, with a similar config file.

     Any clue, ideas, hints on how to proceed?

     Thank you in advance,

     Álvaro

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Picon

[Issue] No member name 'operator->' for std::weak

Hi All,

I encountered a problem after I upgrade swig from 2.0.7 to 3.0.8, the compiler shows that:

error: class std::weak_ptr<T>' has no member named 'operator->’

Since std::weak_ptr has no overloading function of operator->, I wonder why swig generate this code and how to avoid this.

Any suggestions welcome.
Thanks.


Brs,
    mingmin
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mosesson, Daniel B. | 19 Jan 16:21 2016

wrapping a large c++ codebase with default template instatiations

Is there a way to get swig to take in a large set of c++ header files and do something like:

 

For all template functions, create functions of the form fname_typename1

List all functions that could not be completely wrapped

 

I don’t want to write the .i files by hand as there are thousands of lines of headers. Is this possible? I understand that the output may be very verbose, but that is not a problem.

 

Thank you,

Daniel Mosesson

Software Engineer

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Randall McPherson | 19 Jan 04:45 2016
Picon
Gravatar

Vector of class template in Go

I'm trying to wrap the google S2 C++ library with a go interface using swig. When wrapping a typedef of a template class I declared as:

typedef Vector3_d S2Point; 

I get the following error:

'_swig_go_result' declared as a pointer to a reference of type 'std::vector<S2Point>::const_reference' (aka 'const Vector3<double> &')

Vector3_d is declared as:
typedef Vector3_d S2Point;

Do I need to use a typemap to solve this problem? I'm not sure at this point if that's the best way to go about it or if there's a template solution. I'd appreciate any help pointing toward the solution and whether it's a problem with typemapping or not. The swig file can be found here along with the rest of the code: 



------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
JR Heisey | 15 Jan 19:14 2016

SWIG generated cpp for Python and Visual Studio 2015

Greetings,

 

Perhaps this is an obscure issue.

I am slowing progressing my code base to VS2015.

It has a feature where I can use the VS2015 GUI and still build with the compiler from VS2008.

Unfortunately when I do this I get the following error.

 

CdciControllerInterfaces.Python.cpp – this is the SWIG generated file.

1>e:\program files (x86)\microsoft visual studio 9.0\vc\include\use_ansi.h(49): fatal error C1189: #error :  You have included some C++/C library header files with _DEBUG defined and some with _DEBUG not defined. This will not work correctly. Please have _DEBUG set or clear consistently.

 

This code is un-defining the _DEBUG macro while compiling Python.h.

Unfortunate it also includes system headers.

Oddly it does not do this from the VS2008 GUI.

It does not do this when I use the compiler that comes with VS2015.

 

#if defined(_DEBUG) && defined(SWIG_PYTHON_INTERPRETER_NO_DEBUG)

/* Use debug wrappers with the Python release dll */

# undef _DEBUG

# include <Python.h>

# define _DEBUG

#else

# include <Python.h>

#endif

 

My goal is to eventually abandon VS2008 completely.

I have several related project that need to be migrated.

I was hoping to be able to abandon the GUI first before I abandon the compiler.

 

Anyone have any ideas what might be different when compiling in the VS2015 GUI?

 

Thanks,

J.R. Heisey

X21598

 

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Gmane