473,509 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help on python SWIG C++ extension

RLC
Hello

I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly. Below is the code. I am working on FreeBSD 7.0, GCC
4.2.1. I will be very appreciated if you could help me. Thanks a lot.

the error I have got
Segmentation fault: 11 (core dumped)
////////////////////////
///Header file
////////////////////////

#ifndef TDIMPORT_H
#define TDIMPORT_H
#include <string>
#include <vector>
#include "math.h"
#include "float.h"

#define WORDINVALIDVALUE ULONG_MAX
#define FLOATINVALIDVALUE FLT_MAX

typedef unsigned long WORD;

typedef struct
{
double x,y,z;
} Vertex;

extern Vertex *new_Vertex(double x, double y, double z);
extern void delete_Vertex(Vertex *v);
extern double Vertex_length(Vertex *v);

static const Vertex UNDEFINEDVERTEX = {FLT_MAX,FLT_MAX,FLT_MAX};

typedef struct
{
double u,v;
} UV;

extern UV *new_UV(double u, double v);
extern void delete_UV(UV *uv);

static const UV UNDEFINEDUV = {FLT_MAX,FLT_MAX};

typedef struct
{
double red,green,blue,alpha;
} Color;

extern Color *new_Color(double red, double green, double blue, double
alpha);
extern void delete_Color(Color *color);

static const Color BLACK = {0.0, 0.0, 0.0, 1.0};
static const Color WHITE = {1.0, 1.0, 1.0, 1.0};

typedef struct
{
int type;
Color color;
std::string texture;
} Material;

typedef struct
{
std::vector<Vertex vertices;

std::vector<unsigned long polygonNbVertices;

std::vector<unsigned long polygonStartVertexIndex;

std::vector<unsigned long polygonVerticesIndices;

std::vector<UV uvs;

std::vector<unsigned long polygonNbUVs;

std::vector<unsigned long polygonStartUVIndex;

std::vector<unsigned long polygonUVsIndices;

Material material;
} PolygonMesh;

extern PolygonMesh *new_PolygonMesh();
extern void delete_PolygonMesh(PolygonMesh *p);
extern unsigned long PolygonMesh_countvertices(PolygonMesh *p);
extern unsigned long PolygonMesh_countpolygons(PolygonMesh *p);
extern void PolygonMesh_appendvertex(PolygonMesh *p, Vertex *vertex);
extern Vertex PolygonMesh_getvertex(PolygonMesh *p, unsigned long
vertexIndex);
extern void PolygonMesh_appendpolygon(PolygonMesh *p, const
std::vector<unsigned long>& verticesIndices);
extern long PolygonMesh_getpolygonverticescount(PolygonMesh *p,
unsigned long polygonIndex);
extern std::vector<unsigned long>
PolygonMesh_getpolygonverticesindices(PolygonMesh *p, unsigned long
polygonIndex);

#endif
//////////////////////////
//// implementation
//////////////////////////

#include "tdimport.h"

Vertex *new_Vertex(double x, double y, double z)
{
Vertex *v;
v = (Vertex *)malloc(sizeof(Vertex));
v->x = x;
v->y = y;
v->z = z;
return v;
}

void delete_Vertex(Vertex *v)
{
free(v);
}

double Vertex_length(Vertex *v)
{
return sqrt(v->x*v->x+v->y*v->y+v->z*v->z);
}

UV *new_UV(double u, double v)
{
UV *uv;
uv = (UV *)malloc(sizeof(UV));
uv->u = u;
uv->v = v;
return uv;
}

void delete_UV(UV *uv)
{
free(uv);
}

Color *new_Color(double r, double g, double b, double a)
{
Color *color;
color = (Color *)malloc(sizeof(Color));
color->red = r;
color->green = g;
color->blue = b;
color->alpha = a;
return color;
}

void delete_Color(Color *color)
{
free(color);
}

PolygonMesh *new_PolygonMesh()
{
PolygonMesh *p;
p = (PolygonMesh *)malloc(sizeof(PolygonMesh));
p->vertices.clear();
p->polygonNbVertices.clear();
p->polygonStartVertexIndex.clear();
p->polygonVerticesIndices.clear();
p->uvs.clear();
p->polygonNbUVs.clear();
p->polygonStartUVIndex.clear();
p->polygonUVsIndices.clear();
return p;
}

void delete_PolygonMesh(PolygonMesh *p)
{
free(p);
}

unsigned long PolygonMesh_countvertices(PolygonMesh *p)
{
return (unsigned long)p->vertices.size();
}

unsigned long PolygonMesh_countpolygons(PolygonMesh *p)
{
return (unsigned long)p->polygonNbVertices.size();
}

void PolygonMesh_appendvertex(PolygonMesh *p, Vertex *vertex)
{
p->vertices.push_back(*vertex);
}

void PolygonMesh_appendpolygon(PolygonMesh *p, const
std::vector<unsigned long>& verticesIndices)
{
unsigned int i;
for ( i = 0 ; i < verticesIndices.size() ; i++ )
p->polygonVerticesIndices.push_back(verticesIndices. at(i));
p->polygonStartVertexIndex.push_back(p-
>polygonVerticesIndices.size()-verticesIndices.size());
p->polygonNbVertices.push_back(verticesIndices.size( ));
}

Vertex PolygonMesh_getvertex(PolygonMesh *p, unsigned long
vertexIndex)
{
if (vertexIndex < 0 || vertexIndex>=p->vertices.size() )
{
return UNDEFINEDVERTEX;
}
else
{
return p->vertices.at(vertexIndex);
}
}

long PolygonMesh_getpolygonverticescount(PolygonMesh *p, unsigned long
polygonIndex)
{
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVertexIndex.size()))
{
return (long)-1;
}
else
{
return (long)p->polygonNbVertices.at(polygonIndex);
}
}

std::vector<unsigned long>
PolygonMesh_getpolygonverticesindices(PolygonMesh *p, unsigned long
polygonIndex)
{
std::vector<unsigned longtmp;
tmp.clear();
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVertexIndex.size()))
{
return tmp;
}
else
{
unsigned long count = p->polygonNbVertices.at(polygonIndex);
unsigned long start = p->polygonStartVertexIndex.at(polygonIndex);
for (unsigned long i=0; i<count; i++)
tmp.push_back(p->polygonVerticesIndices.at(i+start));
return tmp;
}
}

/////////////////////////
////SWIG interface
/////////////////////////
%module tdimport
%{
#include "tdimport.h"
%}
%include "std_string.i"
%include "std_vector.i"

namespace std {
%template(IntVector) vector<int>;
%template(UIVector) vector<unsigned long>;
%template(VertexVector) vector<Vertex>;
%template(UVVector) vector<UV>;
}

using namespace std;

typedef struct
{
double x,y,z;
%extend
{
Vertex (double,double,double);
~Vertex();
double length();
}
} Vertex;

typedef struct
{
double u,v;
%extend
{
UV (double,double);
~UV();
}
} UV;

typedef struct
{
double red,green,blue,alpha;
%extend
{
Color (double,double,double,double);
~Color();
}
} Color;

%apply const std::string& {std::string* texture};

typedef struct
{
int type;
Color color;
std::string texture;
} Material;

typedef struct
{

std::vector<Vertex vertices;

std::vector<unsigned long polygonNbVertices;

std::vector<unsigned long polygonStartVertexIndex;

std::vector<unsigned long polygonVerticesIndices;

std::vector<UV uvs;

std::vector<unsigned long polygonNbUVs;

std::vector<unsigned long polygonStartUVIndex;

std::vector<unsigned long polygonUVsIndices;

Material material;

%extend
{
PolygonMesh();
~PolygonMesh();
unsigned long countvertices();
unsigned long countpolygons();
void appendvertex(Vertex*);
Vertex getvertex (unsigned long) ;
void appendpolygon(const std::vector<unsigned long>&);
long getpolygonverticescount(unsigned long);
std::vector<unsigned longgetpolygonverticesindices(unsigned
long);
}
} PolygonMesh;
/////////////////////////
////python file
/////////////////////////
#!/usr/local/bin/python

import tdimport

a = tdimport.PolygonMesh()

a.appendvertex(tdimport.Vertex(1.0,0.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,1.0))

a.appendpolygon([1,2,3])

b = a.getpolygonverticescount(0)

print b

del a

////////////////////////////
///compile command
////////////////////////////
swig -c++ -python tdimport.i
g++ -c tdimport.cpp
g++ -c tdimport_wrap.cxx -I/usr/local/include/python2.5
g++ -shared tdimport.o tdimport_wrap.o -o _tdimport.so


Sep 16 '08 #1
3 1698
RLC schrieb:
Hello

I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly. Below is the code. I am working on FreeBSD 7.0, GCC
4.2.1. I will be very appreciated if you could help me. Thanks a lot.

the error I have got
Segmentation fault: 11 (core dumped)
Try starting the program in gdb. This works using
$ gdb python
(gdb) set args myscript.py
(gdb) run

Then when the segfault hits, get a backtrace to see where the actual
problem occurs.

Diez
Sep 16 '08 #2
RLC <ro********@gmail.comwrote:
>
I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly.
That won't cause a segmentation fault. However, I notice that you are
using "malloc" and "free" to allocate and free your objects. "stl" will
use "new" and "delete", and it's not always healthy to mix them. If I were
you, I'd replace this:
Vertex *v;
v = (Vertex *)malloc(sizeof(Vertex));
with this, which is less typing:
Vertex * x = new Vertex;
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 17 '08 #3
RLC
Thanks a lot, Tim and Diez, Merci Beaucoup!!

I strongly suspect the error was caused by the memory operation. I
made some modifications only on PolygonMesh definition. And now I have
no annoying warnings.
PolygonMesh *new_PolygonMesh()
{
PolygonMesh *p = new PolygonMesh;
// p = (PolygonMesh *)malloc(sizeof(PolygonMesh));
// p->vertices.clear();
// p->polygonNbVertices.clear();
// p->polygonStartVertexIndex.clear();
// p->polygonVerticesIndices.clear();
// p->uvs.clear();
// p->polygonNbUVs.clear();
// p->polygonStartUVIndex.clear();
// p->polygonUVsIndices.clear();
return p;
}

void delete_PolygonMesh(PolygonMesh *p)
{
free(p);
}

when I run below script, it works well without any warning
#!/usr/local/bin/python

import tdimport

a = tdimport.PolygonMesh()

a.appendvertex(tdimport.Vertex(0.0,0.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,0.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,0.0))
a.appendvertex(tdimport.Vertex(0.0,1.0,0.0))
a.appendvertex(tdimport.Vertex(0.0,0.0,1.0))
a.appendvertex(tdimport.Vertex(1.0,0.0,1.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,1.0))
a.appendvertex(tdimport.Vertex(0.0,1.0,1.0))

a.appendpolygon([0,3,2,1])
a.appendpolygon([0,1,5,4])
a.appendpolygon([1,2,6,5])
a.appendpolygon([2,3,7,6])
a.appendpolygon([0,4,7,3])
a.appendpolygon([4,5,6,7])

print a.getpolygonverticesindices(0)
print a.getpolygonverticesindices(1)
print a.getpolygonverticesindices(2)
print a.getpolygonverticesindices(3)
print a.getpolygonverticesindices(4)
print a.getpolygonverticesindices(5)

del a
Thanks again. Have a good day!!!

Regards
Roger

On Sep 17, 8:16 am, Tim Roberts <t...@probo.comwrote:
RLC <rogeruc...@gmail.comwrote:
I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly.

That won't cause a segmentation fault. However, I notice that you are
using "malloc" and "free" to allocate and free your objects. "stl" will
use "new" and "delete", and it's not always healthy to mix them. If I were
you, I'd replace this:
Vertex *v;
v = (Vertex *)malloc(sizeof(Vertex));

with this, which is less typing:
Vertex * x = new Vertex;
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Sep 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2075
by: Phil Schmidt | last post by:
I'm attempting to follow the instructions at http://sebsauvage.net/python/mingw.html, without luck. I have Python 2.3, MinGW 3.2.3, ans SWIG 1.3.19. I get an export error on "initexample" when...
0
2484
by: Helmut Zeisel | last post by:
I want to build a static extension of Python using SWIG and VC++ 6.0 as described in http://www.swig.org/Doc1.3/Python.html#n8 for gcc. My file is testerl.i: ========================= %module...
13
3822
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
3
3379
by: PL | last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for example, add 1 to each element) and pass it back to Python. With these building blocks I will be able to figure out all the...
0
1355
by: John Ling | last post by:
Hello, I have been trying to install an application which requires Python: http://chip.dfci.harvard.edu/~wli/MAT/ My environment is AIX 5.2. Below is the section of the setup that has failed,...
6
3955
by: matey | last post by:
I am have version 2.3.4. I want to write a python script to access a secure HTTPS. I tried the following: import urllib urllib.urlopen("https://somesecuresite.com") s = f.read() f.close()
9
1884
by: bressert | last post by:
Hi Everyone, I'm considering about generating some Python Bindings for C++ libraries. What are considered the best tools for doing something like this? I know that there are SWIG, SIP,...
6
3041
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please...
3
3668
by: Soren | last post by:
Hi, I went through the SWIG tutorial for the example named "simple". I managed to get to the first step, creating example_wrap.c using swig, and doing: "gcc -fpic -c example_wrap.c...
0
1040
by: RLC | last post by:
Hello I am new to python SWIG. Recently I wrote a small program trying to import collada files(using colladadom) into python so I could use python cgkit to render them. However, during the...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.