help on python SWIG C++ extension

RLC rogeruclan at gmail.com
Wed Sep 17 08:23:47 EDT 2008


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... at probo.com> wrote:
> RLC <rogeruc... at gmail.com> wrote:
>
> >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... at probo.com
> Providenza & Boekelheide, Inc.




More information about the Python-list mailing list