473,581 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boost Python - C++ class' private static data blown away beforeaccessing in Python?

I've exposed a C++ class to Python using Boost Python. The class,
let's say it's called Entity, contains private static data, which is
an array of strings. Though I think it implements it using MFC's
CPtrArray.

I've also exposed a public function from Entity - let's say it's
called foo. This function accesses the private static data (the string
array).

I have multiple instances of Entity stored in a custom C++ container,
which is also exposed to Python as class EntityList.

In Python, I retrive an Entity from the EntityList:

elist = EntityList()
elist.append(En tity())
elist.append(En tity())

entity = elist.get_at(0)

entity.foo()

But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.

The static data is setup at initialisation - my Python code is only
called long after initialisation is complete.

I added a static dump() function to the Entity class that dumps the
string array, and even if I just do the following in Python:

Entity.dump()

in Python, the private static data is empty. Doing the same from C++
works fine. Weird.

I know this is an impossible question to ask, but can anyone think of
something obvious I need to look into?

Thanks
Jul 4 '08 #1
5 1966
>
In Python, I retrive an Entity from the EntityList:

elist = EntityList()
elist.append(En tity())
elist.append(En tity())

entity = elist.get_at(0)

entity.foo()

But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.
Probably it is a problem of lifetime. What is the signature of append?
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but
destroys the Entity object because it is a temporary and its reference
count went to zero. So the pointer in the list is referring to a
destroyed object, which results in undefined behaviour.

Did you have a look at the lifetime policies of Boost.Python? The
simplest way to workaround the problem is using const reference
arguments, and always use value semantics. If it can result in a
performance penalty, another simple way is using shared_ptr's, which
have their own reference count (different from the one in CPython
lib), but Boost.Python does the magic to make them work together.

HTH,
Giuseppe
Jul 6 '08 #2
Thanks. Maybe it's a DLL boundary issue? I'll look into this too.

On Jul 5, 11:14*pm, Giuseppe Ottaviano <giu...@gmail.c omwrote:
In Python, I retrive an Entity from the EntityList:
elist = EntityList()
elist.append(En tity())
elist.append(En tity())
entity = elist.get_at(0)
entity.foo()
But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.

Probably it is a problem of lifetime. What is the signature of append? *
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but *
destroys the Entity object because it is a temporary and its reference *
count went to zero. So the pointer in the list is referring to a *
destroyed object, which results in undefined behaviour.

Did you have a look at the lifetime policies of Boost.Python? The *
simplest way to workaround the problem is using const reference *
arguments, and always use value semantics. If it can result in a *
performance penalty, another simple way is using shared_ptr's, which *
have their own reference count (different from the one in CPython *
lib), but Boost.Python does the magic to make them work together.

HTH,
Giuseppe
Jul 9 '08 #3
I wonder if it's a DLL boundary problem.

On Jul 5, 11:14*pm, Giuseppe Ottaviano <giu...@gmail.c omwrote:
In Python, I retrive an Entity from the EntityList:
elist = EntityList()
elist.append(En tity())
elist.append(En tity())
entity = elist.get_at(0)
entity.foo()
But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.

Probably it is a problem of lifetime. What is the signature of append? *
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but *
destroys the Entity object because it is a temporary and its reference *
count went to zero. So the pointer in the list is referring to a *
destroyed object, which results in undefined behaviour.

Did you have a look at the lifetime policies of Boost.Python? The *
simplest way to workaround the problem is using const reference *
arguments, and always use value semantics. If it can result in a *
performance penalty, another simple way is using shared_ptr's, which *
have their own reference count (different from the one in CPython *
lib), but Boost.Python does the magic to make them work together.

HTH,
Giuseppe
Jul 9 '08 #4
Could it be a boundary problem? The static data is initialised by the
application. The problem arises when the python module tries to access
it.

On Jul 5, 11:14*pm, Giuseppe Ottaviano <giu...@gmail.c omwrote:
In Python, I retrive an Entity from the EntityList:
elist = EntityList()
elist.append(En tity())
elist.append(En tity())
entity = elist.get_at(0)
entity.foo()
But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.

Probably it is a problem of lifetime. What is the signature of append? *
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but *
destroys the Entity object because it is a temporary and its reference *
count went to zero. So the pointer in the list is referring to a *
destroyed object, which results in undefined behaviour.

Did you have a look at the lifetime policies of Boost.Python? The *
simplest way to workaround the problem is using const reference *
arguments, and always use value semantics. If it can result in a *
performance penalty, another simple way is using shared_ptr's, which *
have their own reference count (different from the one in CPython *
lib), but Boost.Python does the magic to make them work together.

HTH,
Giuseppe
Jul 9 '08 #5
Oops - I didn't see my post so I thought something had gone wrong and
reposted. Apologies for the multiple posts.

On Jul 9, 11:57*am, Stodge <sto...@gmail.c omwrote:
Could it be a boundary problem? The static data is initialised by the
application. The problem arises when the python module tries to access
it.

On Jul 5, 11:14*pm, Giuseppe Ottaviano <giu...@gmail.c omwrote:
In Python, I retrive an Entity from the EntityList:
elist = EntityList()
elist.append(En tity())
elist.append(En tity())
entity = elist.get_at(0)
entity.foo()
But it crashes inside foo() as the private static data is empty; or
rather the string array is empty. I know before that point that the
private static data is valid when accessed earlier by the C++ code as
the program works fine. It just won't work from Python, so somehow the
private static data has been blown away but I can't work out where or
why.
Probably it is a problem of lifetime. What is the signature of append? *
Who deletes the appended Entity in C++ code?
If append takes a raw pointer, Boost.Python copies the pointer but *
destroys the Entity object because it is a temporary and its reference *
count went to zero. So the pointer in the list is referring to a *
destroyed object, which results in undefined behaviour.
Did you have a look at the lifetime policies of Boost.Python? The *
simplest way to workaround the problem is using const reference *
arguments, and always use value semantics. If it can result in a *
performance penalty, another simple way is using shared_ptr's, which *
have their own reference count (different from the one in CPython *
lib), but Boost.Python does the magic to make them work together.
HTH,
Giuseppe
Jul 9 '08 #6

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

Similar topics

0
2122
by: Li Daobing | last post by:
I can't use .def(str(self)) I write a simple example, without `str', I can build it well, but with this one, I can't build //Rational.cpp #include <boost/python.hpp> #include <iostream> using namespace std; using namespace boost::python;
1
5361
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std; int main(int ac, char* av)
0
1759
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems...
112
13789
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and...
0
1085
by: Stou Sandalski | last post by:
Hi, I have a python library created by wrapping the C++ library using Boost.Python, the problem is that the wrappers are not very pythonic.... so I want to add some methods that do not exist in the C+ + implementation, that would create a better Python interface. For example to initialize the data in an object in the library one must...
2
5550
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In...
162
10138
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language,...
0
7783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8132
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8296
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8157
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6533
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3806
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1118
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.