C++ extension

G

Graeme

Hi All,

I guess that this will be a point which has come up in the past, but I can't
find any references to it anywhere. I am writing a Python extension in C++,
and as a part of the extension class I have an STL list of things i.e

class WhatNot
{
public:
PyObject_HEAD;
..
..
private:
list<Thing> foo;
};

However, I have found that this works fine when I use the class from C++
code, but doesn't work with PyObject_New (the list is not properly
initialised and I get a segmentation fault.)

Is there an easy way around this problem?

Cheers,

Graeme
 
P

Paul McGuire

Graeme said:
Hi All,

I guess that this will be a point which has come up in the past, but I can't
find any references to it anywhere. I am writing a Python extension in C++,
and as a part of the extension class I have an STL list of things i.e

class WhatNot
{
public:
PyObject_HEAD;
.
.
private:
list<Thing> foo;
};

However, I have found that this works fine when I use the class from C++
code, but doesn't work with PyObject_New (the list is not properly
initialised and I get a segmentation fault.)

Is there an easy way around this problem?

Cheers,

Graeme

You could try doing your own initialization, using an auto_ptr to manage the
allocated memory, and a ref to alias the auto_ptr, something like:

#include <memory>
#include <list>
using namespace std;

class WN
{
public:
PyObject_HEAD;
..
..
private:
typedef list<Thing> thingList;
auto_ptr<thingList> fooptr;
thingList& foo;

public:
WN() : fooptr(new list<Thing>()), foo(*fooptr) {}
};


-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top