473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Boost::Python - Question about object ownership and lifetime

Hi folks, new to Boost Python and struggling to build a prototype at
work. I thought I'd start with a conceptual question to help clarify
my understanding. I already have a basic prototype working nicely but
I'm having a few issues, which I may post about later.

A brief functional rundown of what I'm trying to prototype. Hopefully
my explanation doesn't get too confusing!

I'm embedding a python module into an application; Python will provide
application logic, responding to events such as timers expiring or the
user clicking widgets.

I have the following classes I'm exporting to Python:

Entity, DerivedEntity, EntityList (contains pointers to Entity).

They all have varying types of copy/constructors, e.g. some are
private, some have non-const copy constructors etc. It's inherited
code that I can't mess with so I'm stuck with it.

So the typical logic flow I'm looking at is:

C++ Application:

- Create an instance of EntityList called entity_list, populate with
many instances of DerivedEntity.
- Create an instance of EntityList called change_list
- Invoke python function passing entity_list and change_list as
parameters

Python:

- For each entity in entity_list:
- Invoke C++ function clone_entity, pass entity as parameter

C++ Application:

- Create clone of entity like this --DerivedEntity* entity_copy
= new DerivedEntity(entity)
- return entity_copy to Python

Back in Python again:

- Apply logic to entity_copy returned from clone_entity C++
function
- If entity_copy updated add to change_list

Back in C++ Application:

- For each new_entity in change_list
- Apply further logic to new_entity
- Delete new_entity pointer
- Clear the change_list

I'm having problems in my prototype - it looks like I have a memory
leak but I'm not sure where. So my question is, what should I be aware
of in this logic flow with regards to object ownership and lifetime?

When I receive entity_copy in Python, returned from clone_entity, what
happens to this object when I insert it into change_list and
subsequently delete the C++ pointer? I don't know how to prove that
the Python object is being deleted and isn't hanging around due to a
lifetime issue,

I guess my main problem is that I'm entirely sure I understand object
ownership - who owns what object when it's created in C++, passed to
Python and then returned back to C++ to be deleted.

So if any of this makes sense, I would greatly appreciated any
suggestions or help. I'm very impressed with Boost Python so far but
my own lack of understanding is holding me back I think.

Thanks again
Mike
Jun 27 '08 #1
0 1289

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

Similar topics

2
by: Steve Knight | last post by:
Hello, I'm new to Boost & Python and I'm diving straight in by trying to write an extension module to a third party library. Foolishness probably, but I don't have much choice! My question...
0
by: Gouda Man | last post by:
I'm planning to write a large program that will have advanced scripting in python in which the scripter will subclass c++ objects to add functionality. this leads me to boost.python (if you dont...
0
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> ...
0
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...
4
by: Shawn McGrath | last post by:
Hi, I'm trying to expose a C++ class' internals to python via boost::python. I can do integer/boolean functions fine, but as soon as I do a string get/set it craps out. ...
0
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...
0
by: devito | last post by:
hi there, for some days i try to build the boost.python tutorial "hello world" without bjam on winxp by using mingw. so i wrote a *.bat-file like the following: // --- snip...
1
by: =?ISO-8859-1?Q?Fr=E9d=E9ric_Degraeve?= | last post by:
Hello, I tried this code with vs7-8 and boost1.34.1-1.35.0 and my python is a 2.4. The call to Cpython works well but boost::python doesn't work. I've got an exception about an access violation...
5
by: Stodge | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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...
0
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
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,...

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.